feat(Go-Tool):创建ColorUtil:颜色输出工具、修改目录层级、添加数据库表字段处理工具(需要已连接数据库)

This commit is contained in:
huangzj
2020-04-27 16:33:17 +08:00
parent 9b0c055471
commit c71f2afe00
9 changed files with 152 additions and 10 deletions
+56
View File
@@ -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 ""
}
+40
View File
@@ -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
}