增加数据表结构查询的方法

This commit is contained in:
白茶清欢 2023-08-16 23:16:35 +08:00
parent 42208f1b2d
commit 73949714fc
3 changed files with 95 additions and 14 deletions

View File

@ -10,6 +10,7 @@ package database
import (
"errors"
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/serialize"
"path/filepath"
"strings"
@ -132,15 +133,15 @@ func (c *client) getCfg(cfgPath string) (*cfgFile, error) {
cfgInfo Database
)
switch fileType {
case FileTypeYaml:
case consts.FileTypeYaml:
fallthrough
case FileTypeYml:
result.Type = FileTypeYaml
case consts.FileTypeYml:
result.Type = consts.FileTypeYaml
if err = serialize.File.ReadYmlContent(cfgPath, &result.Config); nil != err {
return nil, fmt.Errorf("%s 配置文件解析失败, 原因 : %s", cfgPath, err.Error())
}
case FileTypeJson:
result.Type = FileTypeJson
case consts.FileTypeJson:
result.Type = consts.FileTypeJson
if err = serialize.File.ReadJSONContent(cfgPath, &cfgInfo); nil != err {
return nil, fmt.Errorf("%s 配置文件解析失败, 原因 : %s", cfgPath, err.Error())
}

View File

@ -34,15 +34,6 @@ type cfgFile struct {
Config *Database `json:"config"` // 解析之后的配置文件
}
const (
// FileTypeYml tml
FileTypeYml = "yml"
// FileTypeYaml yaml
FileTypeYaml = "yaml"
// FileTypeJson json
FileTypeJson = "json"
)
// Database 数据库配置
//
// Author : go_developer@163.com<白茶清欢>
@ -86,3 +77,48 @@ type Connection struct {
MaxOpen int `json:"max_open" yaml:"max_open"` // 最大打开连接数
MaxIdle int `json:"max_idle" yaml:"max_idle"` // 最大的处理连接数
}
// DescTableItem 表结构的描述
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:45 2023/8/16
type DescTableItem struct {
Default *string `json:"Default"` // 默认值
Extra string `json:"Extra"` // 扩展信息
Field string `json:"field"` // 字段名
Key string `json:"Key"` // 索引信息
Null string `json:"Null"` // 是否允许为NUll
Type string `json:"Type"` // 字段类型
Comment string `json:"Comment"` // 字段注释
}
// ColumnInfo 表字段结构,INFORMATION_SCHEMA.COLUMNS 标的查询结果
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 23:00 2023/8/16
type ColumnInfo struct {
TableCatalog string `json:"TABLE_CATALOG" gorm:"column:TABLE_CATALOG;default:;NOT NULL"` // TABLE_CATALOG
TableSchema string `json:"TABLE_SCHEMA" gorm:"column:TABLE_SCHEMA;default:;NOT NULL"` // TABLE_SCHEMA
TableName string `json:"TABLE_NAME" gorm:"column:TABLE_NAME;default:;NOT NULL"` // TABLE_NAME
ColumnName string `json:"COLUMN_NAME" gorm:"column:COLUMN_NAME;default:;NOT NULL"` // COLUMN_NAME
OrdinalPosition int64 `json:"ORDINAL_POSITION" gorm:"column:ORDINAL_POSITION;default:;NOT NULL"` // ORDINAL_POSITION
ColumnDefault string `json:"COLUMN_DEFAULT" gorm:"column:COLUMN_DEFAULT;default:;{NOT_NULL}"` // COLUMN_DEFAULT
IsNullable string `json:"IS_NULLABLE" gorm:"column:IS_NULLABLE;default:;NOT NULL"` // IS_NULLABLE
DataType string `json:"DATA_TYPE" gorm:"column:DATA_TYPE;default:;NOT NULL"` // DATA_TYPE
CharacterMaximumLength int64 `json:"CHARACTER_MAXIMUM_LENGTH" gorm:"column:CHARACTER_MAXIMUM_LENGTH;default:;{NOT_NULL}"` // CHARACTER_MAXIMUM_LENGTH
CharacterOctetLength int64 `json:"CHARACTER_OCTET_LENGTH" gorm:"column:CHARACTER_OCTET_LENGTH;default:;{NOT_NULL}"` // CHARACTER_OCTET_LENGTH
NumericPrecision int64 `json:"NUMERIC_PRECISION" gorm:"column:NUMERIC_PRECISION;default:;{NOT_NULL}"` // NUMERIC_PRECISION
NumericScale int64 `json:"NUMERIC_SCALE" gorm:"column:NUMERIC_SCALE;default:;{NOT_NULL}"` // NUMERIC_SCALE
DatetimePrecision int64 `json:"DATETIME_PRECISION" gorm:"column:DATETIME_PRECISION;default:;{NOT_NULL}"` // DATETIME_PRECISION
CharacterSetName string `json:"CHARACTER_SET_NAME" gorm:"column:CHARACTER_SET_NAME;default:;{NOT_NULL}"` // CHARACTER_SET_NAME
CollationName string `json:"COLLATION_NAME" gorm:"column:COLLATION_NAME;default:;{NOT_NULL}"` // COLLATION_NAME
ColumnType string `json:"COLUMN_TYPE" gorm:"column:COLUMN_TYPE;default:;NOT NULL"` // COLUMN_TYPE
ColumnKey string `json:"COLUMN_KEY" gorm:"column:COLUMN_KEY;default:;NOT NULL"` // COLUMN_KEY
Extra string `json:"EXTRA" gorm:"column:EXTRA;default:;NOT NULL"` // EXTRA
Privileges string `json:"PRIVILEGES" gorm:"column:PRIVILEGES;default:;NOT NULL"` // PRIVILEGES
ColumnComment string `json:"COLUMN_COMMENT" gorm:"column:COLUMN_COMMENT;default:;NOT NULL"` // COLUMN_COMMENT
IsGenerated string `json:"IS_GENERATED" gorm:"column:IS_GENERATED;default:;NOT NULL"` // IS_GENERATED
GenerationExpression string `json:"GENERATION_EXPRESSION" gorm:"column:GENERATION_EXPRESSION;default:;{NOT_NULL}"` // GENERATION_EXPRESSION
}

View File

@ -93,3 +93,47 @@ func (sd *SystemDao) GetCreateTableSQL(dbInstance *gorm.DB, table string) (strin
}
return fmt.Sprintf("%v", result["Create Table"]), nil
}
// GetTableDesc 获取数据表的描述
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:37 2023/8/16
func (sd *SystemDao) GetTableDesc(dbInstance *gorm.DB, database string, tableName string) ([]*DescTableItem, error) {
var (
err error
result []*DescTableItem
)
if err = dbInstance.Raw("DESC `" + tableName + "`").Scan(&result).Error; nil != err {
return nil, err
}
// 查询comment信息
tableInfo, _ := sd.GetTableInfo(dbInstance, database, tableName)
tableInfoTable := make(map[string]string)
for _, item := range tableInfo {
tableInfoTable[item.ColumnName] = item.ColumnComment
}
for _, item := range result {
item.Comment = tableInfoTable[item.Field]
}
return result, nil
}
// GetTableInfo 查询别熬结构
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 23:10 2023/8/16
func (sd *SystemDao) GetTableInfo(dbInstance *gorm.DB, database string, tableName string) ([]*ColumnInfo, error) {
var (
list []*ColumnInfo
err error
)
err = dbInstance.Table("INFORMATION_SCHEMA.COLUMNS").Where(map[string]string{
"TABLE_SCHEMA": database,
"TABLE_NAME": tableName,
}).Scan(&list).Error
return list, err
}