Browse Source

增加查询表格信息

master
zhenghaorong 2 years ago
parent
commit
3d486e6fb8
2 changed files with 128 additions and 0 deletions
  1. +64
    -0
      chain.go
  2. +64
    -0
      transaction_chain.go

+ 64
- 0
chain.go View File

@ -179,6 +179,70 @@ func (this *Query) Clean() *Query {
return this return this
} }
//获取表格信息
func (this *Query) GetTableInfo(table string) (map[string]interface{}, error) {
field := []string{
"COLUMN_NAME", //字段名
"COLUMN_DEFAULT", //默认值
"DATA_TYPE", //数据类型
"COLUMN_TYPE", //数据类型+长度
"COLUMN_COMMENT", //备注
"IS_NULLABLE", //是否为空
}
sql := "select `" + strings.Join(field, "`,`") + "` from information_schema.COLUMNS where table_name = ? and table_schema = ?"
stmtSql, err := this.conn.Prepare(sql)
if err != nil {
return nil, err
}
list, err := StmtForQueryList(stmtSql, []interface{}{table, this.dbname})
if err != nil {
return nil, err
}
rows := make([]interface{}, 0, len(list))
fieldName := make([]string, 0, len(list))
for _, item := range list {
info := map[string]interface{}{
"name": "",
"column_type": "",
"is_null": true,
"data_type": "",
"comment": "",
"default": "",
}
for _, k := range field {
index := helper.StrFirstToUpper(k)
if v, ok := item[index]; ok {
switch k {
case "COLUMN_NAME":
info["name"] = v
case "COLUMN_DEFAULT":
info["default"] = v
case "DATA_TYPE":
info["data_type"] = v
case "COLUMN_TYPE":
info["column_type"] = helper.ToInt64(v)
case "COLUMN_COMMENT":
info["comment"] = helper.ToInt64(v)
case "IS_NULLABLE":
if v == "NO" {
info["is_null"] = false
}
}
}
}
name := helper.ToStr(info["name"])
if name != "" {
rows = append(rows, info)
fieldName = append(fieldName, name)
}
}
return map[string]interface{}{
"field": fieldName,
"list": rows,
}, nil
}
//构造子查询 //构造子查询
func (this *Query) BuildSelectSql() (map[string]interface{}, error) { func (this *Query) BuildSelectSql() (map[string]interface{}, error) {
if this.dbname == "" && this.table == "" { if this.dbname == "" && this.table == "" {


+ 64
- 0
transaction_chain.go View File

@ -278,6 +278,70 @@ func (this *TxQuery) BuildSelectSql() (map[string]interface{}, error) {
}, nil }, nil
} }
//获取表格信息
func (this *TxQuery) GetTableInfo(table string) (map[string]interface{}, error) {
field := []string{
"COLUMN_NAME", //字段名
"COLUMN_DEFAULT", //默认值
"DATA_TYPE", //数据类型
"COLUMN_TYPE", //数据类型+长度
"COLUMN_COMMENT", //备注
"IS_NULLABLE", //是否为空
}
sql := "select `" + strings.Join(field, "`,`") + "` from information_schema.COLUMNS where table_name = ? and table_schema = ?"
stmtSql, err := this.tx.Prepare(sql)
if err != nil {
return nil, err
}
list, err := StmtForQueryList(stmtSql, []interface{}{table, this.dbname})
if err != nil {
return nil, err
}
rows := make([]interface{}, 0, len(list))
fieldName := make([]string, 0, len(list))
for _, item := range list {
info := map[string]interface{}{
"name": "",
"column_type": "",
"is_null": true,
"data_type": "",
"comment": "",
"default": "",
}
for _, k := range field {
index := helper.StrFirstToUpper(k)
if v, ok := item[index]; ok {
switch k {
case "COLUMN_NAME":
info["name"] = v
case "COLUMN_DEFAULT":
info["default"] = v
case "DATA_TYPE":
info["data_type"] = v
case "COLUMN_TYPE":
info["column_type"] = helper.ToInt64(v)
case "COLUMN_COMMENT":
info["comment"] = helper.ToInt64(v)
case "IS_NULLABLE":
if v == "NO" {
info["is_null"] = false
}
}
}
}
name := helper.ToStr(info["name"])
if name != "" {
rows = append(rows, info)
fieldName = append(fieldName, name)
}
}
return map[string]interface{}{
"field": fieldName,
"list": rows,
}, nil
}
// 拼查询sql // 拼查询sql
func (this *TxQuery) QueryStmt() error { func (this *TxQuery) QueryStmt() error {


Loading…
Cancel
Save