调整整体架构写法
调整db用法 调整日志用法 调整模版
This commit is contained in:
1 parent
734a48dbb8
commit
e2952fa393
137 files changed
+3638
-3544
No files matched your search
@@ -5,9 +5,8 @@ import (
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
orm "go-admin/common/global"
|
||||
"go-admin/tools"
|
||||
config2 "go-admin/tools/config"
|
||||
"go-admin/tools/config"
|
||||
)
|
||||
|
||||
type DBColumns struct {
|
||||
@@ -25,14 +24,14 @@ type DBColumns struct {
|
||||
ColumnComment string `gorm:"column:COLUMN_COMMENT" json:"columnComment"`
|
||||
}
|
||||
|
||||
func (e *DBColumns) GetPage(pageSize int, pageIndex int) ([]DBColumns, int, error) {
|
||||
func (e *DBColumns) GetPage(tx *gorm.DB, pageSize int, pageIndex int) ([]DBColumns, int, error) {
|
||||
var doc []DBColumns
|
||||
var count int64
|
||||
table := new(gorm.DB)
|
||||
|
||||
if config2.DatabaseConfig.Driver == "mysql" {
|
||||
table = orm.Eloquent.Table("information_schema.`COLUMNS`")
|
||||
table = table.Where("table_schema= ? ", config2.GenConfig.DBName)
|
||||
if config.DatabaseConfig.Driver == "mysql" {
|
||||
table = tx.Table("information_schema.`COLUMNS`")
|
||||
table = table.Where("table_schema= ? ", config.GenConfig.DBName)
|
||||
|
||||
if e.TableName != "" {
|
||||
return nil, 0, errors.New("table name cannot be empty!")
|
||||
@@ -49,7 +48,7 @@ func (e *DBColumns) GetPage(pageSize int, pageIndex int) ([]DBColumns, int, erro
|
||||
|
||||
}
|
||||
|
||||
func (e *DBColumns) GetList() ([]DBColumns, error) {
|
||||
func (e *DBColumns) GetList(tx *gorm.DB) ([]DBColumns, error) {
|
||||
var doc []DBColumns
|
||||
table := new(gorm.DB)
|
||||
|
||||
@@ -57,9 +56,9 @@ func (e *DBColumns) GetList() ([]DBColumns, error) {
|
||||
return nil, errors.New("table name cannot be empty!")
|
||||
}
|
||||
|
||||
if config2.DatabaseConfig.Driver == "mysql" {
|
||||
table = orm.Eloquent.Table("information_schema.columns")
|
||||
table = table.Where("table_schema= ? ", config2.GenConfig.DBName)
|
||||
if config.DatabaseConfig.Driver == "mysql" {
|
||||
table = tx.Table("information_schema.columns")
|
||||
table = table.Where("table_schema= ? ", config.GenConfig.DBName)
|
||||
|
||||
table = table.Where("TABLE_NAME = ?", e.TableName).Order("ORDINAL_POSITION asc")
|
||||
} else {
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
orm "go-admin/common/global"
|
||||
"go-admin/tools"
|
||||
config2 "go-admin/tools/config"
|
||||
)
|
||||
@@ -20,13 +19,13 @@ type DBTables struct {
|
||||
TableComment string `gorm:"column:TABLE_COMMENT" json:"tableComment"`
|
||||
}
|
||||
|
||||
func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int, error) {
|
||||
func (e *DBTables) GetPage(tx *gorm.DB, pageSize int, pageIndex int) ([]DBTables, int, error) {
|
||||
var doc []DBTables
|
||||
table := new(gorm.DB)
|
||||
var count int64
|
||||
|
||||
if config2.DatabaseConfig.Driver == "mysql" {
|
||||
table = orm.Eloquent.Table("information_schema.tables")
|
||||
table = tx.Table("information_schema.tables")
|
||||
table = table.Where("TABLE_NAME not in (select table_name from `" + config2.GenConfig.DBName + "`.sys_tables) ")
|
||||
table = table.Where("table_schema= ? ", config2.GenConfig.DBName)
|
||||
|
||||
@@ -44,21 +43,20 @@ func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int, error)
|
||||
return doc, int(count), nil
|
||||
}
|
||||
|
||||
func (e *DBTables) Get() (DBTables, error) {
|
||||
func (e *DBTables) Get(tx *gorm.DB) (DBTables, error) {
|
||||
var doc DBTables
|
||||
table := new(gorm.DB)
|
||||
if config2.DatabaseConfig.Driver == "mysql" {
|
||||
table = orm.Eloquent.Table("information_schema.tables")
|
||||
table := tx.Table("information_schema.tables")
|
||||
table = table.Where("table_schema= ? ", config2.GenConfig.DBName)
|
||||
if e.TableName == "" {
|
||||
return doc, errors.New("table name cannot be empty!")
|
||||
}
|
||||
table = table.Where("TABLE_NAME = ?", e.TableName)
|
||||
if err := table.First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
} else {
|
||||
tools.Assert(true, "目前只支持mysql数据库", 500)
|
||||
}
|
||||
if err := table.First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package tools
|
||||
|
||||
import (
|
||||
"go-admin/app/admin/models"
|
||||
orm "go-admin/common/global"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SysColumns struct {
|
||||
@@ -51,10 +51,10 @@ func (SysColumns) TableName() string {
|
||||
return "sys_columns"
|
||||
}
|
||||
|
||||
func (e *SysColumns) GetList() ([]SysColumns, error) {
|
||||
func (e *SysColumns) GetList(tx *gorm.DB) ([]SysColumns, error) {
|
||||
var doc []SysColumns
|
||||
|
||||
table := orm.Eloquent.Table("sys_columns")
|
||||
table := tx.Table("sys_columns")
|
||||
|
||||
table = table.Where("table_id = ?", e.TableId)
|
||||
|
||||
@@ -64,9 +64,9 @@ func (e *SysColumns) GetList() ([]SysColumns, error) {
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *SysColumns) Create() (SysColumns, error) {
|
||||
func (e *SysColumns) Create(tx *gorm.DB) (SysColumns, error) {
|
||||
var doc SysColumns
|
||||
result := orm.Eloquent.Table("sys_columns").Create(&e)
|
||||
result := tx.Table("sys_columns").Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
@@ -75,14 +75,14 @@ func (e *SysColumns) Create() (SysColumns, error) {
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *SysColumns) Update() (update SysColumns, err error) {
|
||||
if err = orm.Eloquent.Table("sys_columns").First(&update, e.ColumnId).Error; err != nil {
|
||||
func (e *SysColumns) Update(tx *gorm.DB) (update SysColumns, err error) {
|
||||
if err = tx.Table("sys_columns").First(&update, e.ColumnId).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_columns").Model(&update).Updates(&e).Error; err != nil {
|
||||
if err = tx.Table("sys_columns").Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
|
||||
"go-admin/app/admin/models"
|
||||
orm "go-admin/common/global"
|
||||
)
|
||||
|
||||
type SysTables struct {
|
||||
TableId int `gorm:"primaryKey;autoIncrement" json:"tableId"` //表编码
|
||||
TableId int `gorm:"primaryKey;autoIncrement" json:"tableId"` //表编码
|
||||
TBName string `gorm:"column:table_name;size:255;" json:"tableName"` //表名称
|
||||
TableComment string `gorm:"size:255;" json:"tableComment"` //表备注
|
||||
ClassName string `gorm:"size:255;" json:"className"` //类名
|
||||
@@ -53,10 +53,10 @@ type Params struct {
|
||||
TreeName string `gorm:"-" json:"treeName"`
|
||||
}
|
||||
|
||||
func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int, error) {
|
||||
func (e *SysTables) GetPage(tx *gorm.DB, pageSize int, pageIndex int) ([]SysTables, int, error) {
|
||||
var doc []SysTables
|
||||
|
||||
table := orm.Eloquent.Table("sys_tables")
|
||||
table := tx.Table("sys_tables")
|
||||
|
||||
if e.TBName != "" {
|
||||
table = table.Where("table_name = ?", e.TBName)
|
||||
@@ -74,10 +74,10 @@ func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int, erro
|
||||
return doc, int(count), nil
|
||||
}
|
||||
|
||||
func (e *SysTables) Get() (SysTables, error) {
|
||||
func (e *SysTables) Get(tx *gorm.DB) (SysTables, error) {
|
||||
var doc SysTables
|
||||
var err error
|
||||
table := orm.Eloquent.Table("sys_tables")
|
||||
table := tx.Table("sys_tables")
|
||||
|
||||
if e.TBName != "" {
|
||||
table = table.Where("table_name = ?", e.TBName)
|
||||
@@ -94,17 +94,17 @@ func (e *SysTables) Get() (SysTables, error) {
|
||||
}
|
||||
var col SysColumns
|
||||
col.TableId = doc.TableId
|
||||
if doc.Columns, err = col.GetList(); err != nil {
|
||||
if doc.Columns, err = col.GetList(tx); err != nil {
|
||||
return doc, err
|
||||
}
|
||||
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *SysTables) GetTree() ([]SysTables, error) {
|
||||
func (e *SysTables) GetTree(tx *gorm.DB) ([]SysTables, error) {
|
||||
var doc []SysTables
|
||||
var err error
|
||||
table := orm.Eloquent.Table("sys_tables")
|
||||
table := tx.Table("sys_tables")
|
||||
|
||||
if e.TBName != "" {
|
||||
table = table.Where("table_name = ?", e.TBName)
|
||||
@@ -123,7 +123,7 @@ func (e *SysTables) GetTree() ([]SysTables, error) {
|
||||
var col SysColumns
|
||||
//col.FkCol = append(col.FkCol, SysColumns{ColumnId: 0, ColumnName: "请选择"})
|
||||
col.TableId = doc[i].TableId
|
||||
if doc[i].Columns, err = col.GetList(); err != nil {
|
||||
if doc[i].Columns, err = col.GetList(tx); err != nil {
|
||||
return doc, err
|
||||
}
|
||||
|
||||
@@ -132,9 +132,9 @@ func (e *SysTables) GetTree() ([]SysTables, error) {
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *SysTables) Create() (SysTables, error) {
|
||||
func (e *SysTables) Create(tx *gorm.DB) (SysTables, error) {
|
||||
var doc SysTables
|
||||
result := orm.Eloquent.Table("sys_tables").Create(&e)
|
||||
result := tx.Table("sys_tables").Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
@@ -143,20 +143,20 @@ func (e *SysTables) Create() (SysTables, error) {
|
||||
for i := 0; i < len(e.Columns); i++ {
|
||||
e.Columns[i].TableId = doc.TableId
|
||||
|
||||
e.Columns[i].Create()
|
||||
_, _ = e.Columns[i].Create(tx)
|
||||
}
|
||||
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *SysTables) Update() (update SysTables, err error) {
|
||||
func (e *SysTables) Update(tx *gorm.DB) (update SysTables, err error) {
|
||||
//if err = orm.Eloquent.Table("sys_tables").First(&update, e.TableId).Error; err != nil {
|
||||
// return
|
||||
//}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_tables").Where("table_id = ?", e.TableId).Updates(&e).Error; err != nil {
|
||||
if err = tx.Table("sys_tables").Where("table_id = ?", e.TableId).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ func (e *SysTables) Update() (update SysTables, err error) {
|
||||
tables := make([]SysTables, 0)
|
||||
tableMap := make(map[string]*SysTables)
|
||||
if len(tableNames) > 0 {
|
||||
if err = orm.Eloquent.Table("sys_tables").Where("table_name in (?)", tableNames).Find(&tables).Error; err != nil {
|
||||
if err = tx.Table("sys_tables").Where("table_name in (?)", tableNames).Find(&tables).Error; err != nil {
|
||||
return
|
||||
}
|
||||
for i := range tables {
|
||||
@@ -196,13 +196,13 @@ func (e *SysTables) Update() (update SysTables, err error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
_, _ = e.Columns[i].Update()
|
||||
_, _ = e.Columns[i].Update(tx)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *SysTables) Delete() (success bool, err error) {
|
||||
tx := orm.Eloquent.Begin()
|
||||
func (e *SysTables) Delete(db *gorm.DB) (success bool, err error) {
|
||||
tx := db.Begin()
|
||||
defer func() {
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
@@ -222,8 +222,8 @@ func (e *SysTables) Delete() (success bool, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (e *SysTables) BatchDelete(id []int) (Result bool, err error) {
|
||||
if err = orm.Eloquent.Unscoped().Table(e.TableName()).Where(" table_id in (?)", id).Delete(&SysColumns{}).Error; err != nil {
|
||||
func (e *SysTables) BatchDelete(tx *gorm.DB, id []int) (Result bool, err error) {
|
||||
if err = tx.Unscoped().Table(e.TableName()).Where(" table_id in (?)", id).Delete(&SysColumns{}).Error; err != nil {
|
||||
return
|
||||
}
|
||||
Result = true
|
||||
|
||||
Reference in new issue
Block a user