code generation tool

This commit is contained in:
zhangwenjian committed 2020-03-19 22:04:38 +08:00
1 parent 1c650ce539
commit 1b03cf2c48
7 files changed
+261 -79

No files matched your search

+32 -14
View File
@@ -9,28 +9,28 @@ type DBColumns struct {
//select COLUMN_NAME,COLUMN_DEFAULT,IS_NULLABLE,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,CHARACTER_SET_NAME,COLUMN_TYPE,COLUMN_KEY,EXTRA,COLUMN_COMMENT
//from information_schema.`COLUMNS` where table_schema='test1db' and TABLE_NAME='sys_config'
TableSchema string `gorm:"column:TABLE_SCHEMA" json:"tableSchema"`
TableName string `gorm:"column:TABLE_NAME" json:"tableName"`
Engine string `gorm:"column:COLUMN_NAME" json:"engine"`
TableRows string `gorm:"column:COLUMN_DEFAULT" json:"tableRows"`
TableCollation string `gorm:"column:IS_NULLABLE" json:"tableCollation"`
CreateTime string `gorm:"column:DATA_TYPE" json:"createTime"`
UpdateTime string `gorm:"column:CHARACTER_MAXIMUM_LENGTH" json:"updateTime"`
TableComment string `gorm:"column:CHARACTER_SET_NAME" json:"tableComment"`
ColumnType string `gorm:"column:COLUMN_TYPE" json:"columnType"`
ColumnKey string `gorm:"column:COLUMN_KEY" json:"columnKey"`
Extra string `gorm:"column:EXTRA" json:"extra"`
ColumnComment string `gorm:"column:COLUMN_COMMENT" json:"columnComment"`
TableSchema string `gorm:"column:TABLE_SCHEMA" json:"tableSchema"`
TableName string `gorm:"column:TABLE_NAME" json:"tableName"`
ColumnName string `gorm:"column:COLUMN_NAME" json:"columnName"`
ColumnDefault string `gorm:"column:COLUMN_DEFAULT" json:"columnDefault"`
IsNullable string `gorm:"column:IS_NULLABLE" json:"isNullable"`
DataType string `gorm:"column:DATA_TYPE" json:"dataType"`
CharacterMaximumLength string `gorm:"column:CHARACTER_MAXIMUM_LENGTH" json:"characterMaximumLength"`
CharacterSetName string `gorm:"column:CHARACTER_SET_NAME" json:"characterSetName"`
ColumnType string `gorm:"column:COLUMN_TYPE" json:"columnType"`
ColumnKey string `gorm:"column:COLUMN_KEY" json:"columnKey"`
Extra string `gorm:"column:EXTRA" json:"extra"`
ColumnComment string `gorm:"column:COLUMN_COMMENT" json:"columnComment"`
}
func (e *DBColumns) GetPage(pageSize int, pageIndex int) ([]DBColumns, int32, error) {
var doc []DBColumns
table := orm.Eloquent.Select("*").Table("information_schema.`COLUMNS`")
table=table.Where("table_schema='test1db'")
table = table.Where("table_schema='test1db'")
if e.TableName != "" {
return nil,0,errors.New("table name cannot be empty!")
return nil, 0, errors.New("table name cannot be empty!")
}
table = table.Where("TABLE_NAME = ?", e.TableName)
@@ -43,3 +43,21 @@ func (e *DBColumns) GetPage(pageSize int, pageIndex int) ([]DBColumns, int32, er
table.Count(&count)
return doc, count, nil
}
func (e *DBColumns) GetList() ([]DBColumns, error) {
var doc []DBColumns
table := orm.Eloquent.Select("*").Table("information_schema.columns")
table = table.Where("table_schema='test1db'")
if e.TableName == "" {
return nil, errors.New("table name cannot be empty!")
}
table = table.Where("TABLE_NAME = ?", e.TableName)
if err := table.Find(&doc).Error; err != nil {
return doc, err
}
return doc, nil
}
+25 -8
View File
@@ -1,24 +1,25 @@
package tools
import (
"errors"
orm "go-admin/database"
)
type DBTables struct {
TableName string `gorm:"column:TABLE_NAME" json:"tableName"`
Engine string `gorm:"column:ENGINE" json:"engine"`
TableRows string `gorm:"column:TABLE_ROWS" json:"tableRows"`
TableName string `gorm:"column:TABLE_NAME" json:"tableName"`
Engine string `gorm:"column:ENGINE" json:"engine"`
TableRows string `gorm:"column:TABLE_ROWS" json:"tableRows"`
TableCollation string `gorm:"column:TABLE_COLLATION" json:"tableCollation"`
CreateTime string `gorm:"column:CREATE_TIME" json:"createTime"`
UpdateTime string `gorm:"column:UPDATE_TIME" json:"updateTime"`
TableComment string `gorm:"column:TABLE_COMMENT" json:"tableComment"`
CreateTime string `gorm:"column:CREATE_TIME" json:"createTime"`
UpdateTime string `gorm:"column:UPDATE_TIME" json:"updateTime"`
TableComment string `gorm:"column:TABLE_COMMENT" json:"tableComment"`
}
func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int32, error) {
var doc []DBTables
table := orm.Eloquent.Select("*").Table("information_schema.tables")
table=table.Where("table_schema='test1db'")
table = table.Where("table_schema='test1db'")
if e.TableName != "" {
table = table.Where("TABLE_NAME = ?", e.TableName)
@@ -31,4 +32,20 @@ func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int32, erro
}
table.Count(&count)
return doc, count, nil
}
}
func (e *DBTables) Get() (DBTables, error) {
var doc DBTables
table := orm.Eloquent.Select("*").Table("information_schema.tables")
table = table.Where("table_schema='test1db'")
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
}
return doc, nil
}
+19 -4
View File
@@ -1,11 +1,14 @@
package tools
import orm "go-admin/database"
import (
orm "go-admin/database"
"go-admin/utils"
)
type SysColumns struct {
ColumnId int64 `gorm:"column:configId;primary_key" json:"configId"`
TableId int64 `gorm:"column:configId" json:"configId"`
ColumnName string `gorm:"column:configId" json:"configId"`
ColumnId int64 `gorm:"column:column_id;primary_key" json:"columnId"`
TableId int64 `gorm:"column:table_id" json:"tableId"`
ColumnName string `gorm:"column:column_name" json:"columnName"`
ColumnComment string `gorm:"column:column_comment" json:"columnComment"`
ColumnType string `gorm:"column:column_type" json:"columnType"`
GoType string `gorm:"column:go_type" json:"goType"`
@@ -50,3 +53,15 @@ func (e *SysColumns) GetList() ([]SysColumns, error) {
}
return doc, nil
}
func (e *SysColumns) Create() (SysColumns, error) {
var doc SysColumns
e.CreateTime = utils.GetCurrntTime()
result := orm.Eloquent.Table("sys_columns").Create(&e)
if result.Error != nil {
err := result.Error
return doc, err
}
doc = *e
return doc, nil
}
+32 -13
View File
@@ -7,7 +7,7 @@ import (
type SysTables struct {
//表编码
TableId string `gorm:"column:table_id" json:"tableName"`
TableId int64 `gorm:"column:table_id;primary_key" json:"tableId"`
//表名称
TableName string `gorm:"column:table_name" json:"tableName"`
//表备注
@@ -34,12 +34,13 @@ type SysTables struct {
Crud bool `gorm:"column:crud" json:"crud"`
Remark string `gorm:"column:remark" json:"remark"`
CreateBy string `gorm:"column:create_by" json:"createBy"`
CreateTime string `gorm:"column:create_time" json:"createTime"`
UpdateBy string `gorm:"column:update_By" json:"updateBy"`
UpdateTime string `gorm:"column:update_time" json:"updateTime"`
DataScope string `gorm:"-" json:"dataScope"`
Params string `gorm:"-" json:"params"`
CreateBy string `gorm:"column:create_by" json:"createBy"`
CreateTime string `gorm:"column:create_time" json:"createTime"`
UpdateBy string `gorm:"column:update_By" json:"updateBy"`
UpdateTime string `gorm:"column:update_time" json:"updateTime"`
DataScope string `gorm:"-" json:"dataScope"`
Params string `gorm:"-" json:"params"`
Columns []SysColumns `gorm:"-" json:"columns"`
}
func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int32, error) {
@@ -50,7 +51,7 @@ func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int32, er
if e.TableName != "" {
table = table.Where("table_name = ?", e.TableName)
}
if e.TableComment !=""{
if e.TableComment != "" {
table = table.Where("table_comment = ?", e.TableComment)
}
@@ -65,20 +66,28 @@ func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int32, er
func (e *SysTables) Get() (SysTables, error) {
var doc SysTables
var err error
table := orm.Eloquent.Select("*").Table("sys_tables")
if e.TableName != "" {
table = table.Where("table_name = ?", e.TableName)
}
if e.TableComment !=""{
if e.TableId != 0 {
table = table.Where("table_id = ?", e.TableId)
}
if e.TableComment != "" {
table = table.Where("table_comment = ?", e.TableComment)
}
if err := table.First(&doc).Error; err != nil {
return doc, err
}
var col SysColumns
col.TableId = e.TableId
if doc.Columns, err = col.GetList(); err != nil {
return doc, err
}
return doc, nil
}
@@ -91,6 +100,12 @@ func (e *SysTables) Create() (SysTables, error) {
return doc, err
}
doc = *e
for i := 0; i < len(e.Columns); i++ {
e.Columns[i].TableId = doc.TableId
e.Columns[i].Create()
}
return doc, nil
}
@@ -109,10 +124,14 @@ func (e *SysTables) Update() (update SysTables, err error) {
}
func (e *SysTables) Delete() (success bool, err error) {
if err = orm.Eloquent.Table("sys_tables").Delete("table_id = ?", e.TableId).Error; err != nil {
if err = orm.Eloquent.Table("sys_tables").Delete(SysTables{}, "table_id = ?", e.TableId).Error; err != nil {
success = false
return
}
if err = orm.Eloquent.Table("sys_columns").Delete(SysColumns{}, "table_id = ?", e.TableId).Error; err != nil {
success = false
return
}
success = true
return
}
}