diff --git a/chain.go b/chain.go index 969db52..f0ea72e 100644 --- a/chain.go +++ b/chain.go @@ -179,6 +179,70 @@ func (this *Query) Clean() *Query { 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) { if this.dbname == "" && this.table == "" { diff --git a/transaction_chain.go b/transaction_chain.go index 0cd3e6c..4e27911 100644 --- a/transaction_chain.go +++ b/transaction_chain.go @@ -278,6 +278,70 @@ func (this *TxQuery) BuildSelectSql() (map[string]interface{}, error) { }, 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 func (this *TxQuery) QueryStmt() error {