diff --git a/MainTest.go b/MainTest.go index 62b9971..5854da9 100644 --- a/MainTest.go +++ b/MainTest.go @@ -7,7 +7,7 @@ package main import ( - "Go-Tool/util" + "Go-Tool/util/reflectM" "fmt" "reflect" ) @@ -22,25 +22,25 @@ func testReflect() { S: "1223", T: make(map[string]string), } - structMap := util.GetStructFieldTypeMap(entity) + structMap := reflectM.GetStructFieldTypeMap(entity) fmt.Println("根据结构体获取Map结构") for key, val := range structMap { fmt.Println(fmt.Sprintf("结构体属性,key: %v ,value: %v ", key, val)) } - strMap := util.GetPtrFieldTypeMap(&entity) + strMap := reflectM.GetPtrFieldTypeMap(&entity) fmt.Println("根据结构体的指针对象获取Map结构") for key, val := range strMap { fmt.Println(fmt.Sprintf("结构体属性,key: %v ,value: %v ", key, val)) } - ptrList := util.GetPtrTypeFieldNameList(reflect.TypeOf(&entity)) + ptrList := reflectM.GetPtrTypeFieldNameList(reflect.TypeOf(&entity)) fmt.Println("根据结构体指针类型获取对应的属性名") for _, val := range ptrList { fmt.Println(fmt.Sprintf("字段名:%s ", val)) } - structList := util.GetStructTypeFieldNameList(reflect.TypeOf(entity)) + structList := reflectM.GetStructTypeFieldNameList(reflect.TypeOf(entity)) fmt.Println("根据结构体类型获取对应所有字段名称") for _, val := range structList { fmt.Println(fmt.Sprintf("字段名:%s ", val)) diff --git a/readme.md b/readme.md index 8809dbc..0350cfb 100644 --- a/readme.md +++ b/readme.md @@ -6,4 +6,6 @@ # 更新日志 2020/4/24 :创建FiledValueTool.go四个方法,分别处理【结构体对象】、【结构体指针对象】的字段名-字段值的map获取以及 【结构体类型】、【结构体指针类型】的字段名获取 -2020/4/27 :创建FileUtil.go、FileReadTool.go、FileWriteTool.go文件,添加文件文件夹操作、读写文件操作方法 \ No newline at end of file +2020/4/27 :创建FileUtil.go、FileReadTool.go、FileWriteTool.go文件,添加文件文件夹操作、读写文件操作方法 + +2020/4/27 :创建ColorUtil:颜色输出工具、修改目录层级、添加数据库表字段处理工具(需要已连接数据库) \ No newline at end of file diff --git a/util/ColorUtil.go b/util/ColorUtil.go new file mode 100644 index 0000000..06dbca8 --- /dev/null +++ b/util/ColorUtil.go @@ -0,0 +1,44 @@ +/* + * @Author : huangzj + * @Time : 2020/4/17 14:04 + * @Description:控制台输出字体颜色工具,根据网上的内容修改,忘了对应的代码地址... + */ + +package util + +import "syscall" + +var ( + kernel32 *syscall.LazyDLL = syscall.NewLazyDLL(`kernel32.dll`) + proc *syscall.LazyProc = kernel32.NewProc(`SetConsoleTextAttribute`) + CloseHandle *syscall.LazyProc = kernel32.NewProc(`CloseHandle`) + + // 给字体颜色对象赋值 + FontColor Color = Color{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} +) + +type Color struct { + black int // 黑色 + blue int // 蓝色 + green int // 绿色 + cyan int // 青色 + Red int // 红色 + purple int // 紫色 + yellow int // 黄色 + light_gray int // 淡灰色(系统默认值) + gray int // 灰色 + light_blue int // 亮蓝色 + light_green int // 亮绿色 + light_cyan int // 亮青色 + light_red int // 亮红色 + light_purple int // 亮紫色 + light_yellow int // 亮黄色 + white int // 白色 +} + +// 输出有颜色的字体 +func ColorPrint(s string, i int) { + handle, _, _ := proc.Call(uintptr(syscall.Stdout), uintptr(i)) + print(s) + CloseHandle.Call(handle) +} diff --git a/util/FileReadTool.go b/util/file/FileReadTool.go similarity index 99% rename from util/FileReadTool.go rename to util/file/FileReadTool.go index 2048fa9..02f5785 100644 --- a/util/FileReadTool.go +++ b/util/file/FileReadTool.go @@ -4,7 +4,7 @@ * @Description: 文件读取工具 */ -package util +package file import ( err2 "Go-Tool/err" diff --git a/util/FileUtil.go b/util/file/FileUtil.go similarity index 99% rename from util/FileUtil.go rename to util/file/FileUtil.go index 5084ef8..dec8b15 100644 --- a/util/FileUtil.go +++ b/util/file/FileUtil.go @@ -16,7 +16,7 @@ * */ -package util +package file import ( err2 "Go-Tool/err" diff --git a/util/FileWriteTool.go b/util/file/FileWriteTool.go similarity index 98% rename from util/FileWriteTool.go rename to util/file/FileWriteTool.go index 7f4800b..ed42089 100644 --- a/util/FileWriteTool.go +++ b/util/file/FileWriteTool.go @@ -4,7 +4,7 @@ * @Description: 文件写入工具 */ -package util +package file import ( err2 "Go-Tool/err" diff --git a/util/FiledValueTool.go b/util/reflectM/FiledValueTool.go similarity index 99% rename from util/FiledValueTool.go rename to util/reflectM/FiledValueTool.go index 5ee0598..04f6c72 100644 --- a/util/FiledValueTool.go +++ b/util/reflectM/FiledValueTool.go @@ -4,7 +4,7 @@ * @Description: */ -package util +package reflectM import "reflect" diff --git a/util/tableField/ColumnMessage.go b/util/tableField/ColumnMessage.go new file mode 100644 index 0000000..984b5c5 --- /dev/null +++ b/util/tableField/ColumnMessage.go @@ -0,0 +1,56 @@ +package tableField + +import "database/sql" + +type ColumnMessage struct { + Field string //字段名称 + Type string //字段类型 + Collation sql.NullString //编码信息 + Null sql.NullString //是否允许为空 + Key sql.NullString //是否是主键 + Default sql.NullString //默认值 + Extra sql.NullString //不懂 + Privileges sql.NullString //执行权限 + Comment sql.NullString //注释信息 +} + +/* + 返回结果判断是不是主键 +*/ +func (c *ColumnMessage) IsKey() bool { + if c.Key.Valid && c.Key.String == "PRI" { + return true + } + return false +} + +/* + 返回结果是否为空信息 +*/ +func (c *ColumnMessage) CanNull() bool { + if c.Null.Valid && c.Null.String == "NO" { + return false + } + return true +} + +/* + 返回注释信息,没有注释信息返回空字符串 +*/ +func (c *ColumnMessage) GetComment() string { + if c.Comment.Valid { + return c.Comment.String + } + + return "" +} + +/* + 返回默认值信息 +*/ +func (c *ColumnMessage) GetDefault() string { + if c.Default.Valid { + return c.Default.String + } + return "" +} diff --git a/util/tableField/TableFieldUtil.go b/util/tableField/TableFieldUtil.go new file mode 100644 index 0000000..9916f53 --- /dev/null +++ b/util/tableField/TableFieldUtil.go @@ -0,0 +1,40 @@ +/* + * @Author : huangzj + * @Time : 2020/4/27 11:43 + * @Description: + */ + +package tableField + +import ( + "database/sql" + "log" + "strings" +) + +/* + * @param + * @return + * @description 在连接数据库的情况下进行数据库表的字段信息查询 + * 这边的sql查询语句是:show full columns from [数据库名].[表名] + */ +func FindColumnMessage(dbName string, tableName string, db *sql.DB) ([]ColumnMessage, error) { + var cms []ColumnMessage + + row, err := db.Query(strings.Join([]string{"show full columns from ", dbName, ".", tableName}, "")) + if err != nil { + log.Printf("数据库查询出错:" + err.Error()) + return nil, err + } + + for row.Next() { + var cm ColumnMessage + err := row.Scan(&cm.Field, &cm.Type, &cm.Collation, &cm.Null, &cm.Key, &cm.Default, &cm.Extra, &cm.Privileges, &cm.Comment) + if err != nil { + log.Printf("字段赋值错误了" + err.Error()) + return nil, err + } + cms = append(cms, cm) + } + return cms, nil +}