From 42ed326eaa48df965b4958667b5c8c13af6be04f Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 10 Jul 2018 22:35:47 +0800 Subject: [PATCH] tce add mdb.show.extras --- src/contexts/cli/cli.go | 4 ++++ src/contexts/ctx.go | 39 ++++++++++++++++++++++++++++++++------- src/contexts/mdb/mdb.go | 26 +++++++++++++++++++++----- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index 5a9c58fd..539afd09 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -141,6 +141,7 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ yac.Cmd("train", "stm", "function", "function", "rep{", "key", "}") yac.Cmd("train", "stm", "return", "return", "rep{", "exp", "}") + yac.Cmd("train", "cmd", "echo", "rep{", "exp", "}") yac.Cmd("train", "cmd", "cmd", "cache", "rep{", "word", "}") yac.Cmd("train", "cmd", "cmd", "cache", "key", "rep{", "word", "}") yac.Cmd("train", "cmd", "cmd", "cache", "key", "opt{", "=", "exp", "}") @@ -833,6 +834,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", m.Echo("%d\n", i) } }}, + "echo": &ctx.Command{Name: "echo arg...", Help: "函数调用, name: 函数名, arg: 参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Echo("%s", strings.Join(arg, "")) + }}, }, Index: map[string]*ctx.Context{ "void": &ctx.Context{Name: "void", diff --git a/src/contexts/ctx.go b/src/contexts/ctx.go index 927ff35e..ead15a57 100644 --- a/src/contexts/ctx.go +++ b/src/contexts/ctx.go @@ -1198,9 +1198,34 @@ func (m *Message) Table(cb func(map[string]string, []string, int) bool) *Message for _, k := range m.Meta["append"] { data := m.Meta[k][i] if m.Options("extras") && k == "extra" { - extra := map[string]interface{}{} + var extra interface{} json.Unmarshal([]byte(data), &extra) - data = fmt.Sprintf("%v", extra[m.Option("extras")]) + for _, k := range m.Meta["extras"] { + if i, e := strconv.Atoi(k); e == nil && i >= 0 { + if d, ok := extra.([]interface{}); ok && i < len(d) { + extra = d[i] + continue + } + } + + if d, ok := extra.(map[string]interface{}); ok { + extra = d[k] + continue + } + + extra = nil + break + } + + if extra == nil { + data = "" + } else { + format := m.Confx("extra_format") + if format == "" { + format = "%v" + } + data = fmt.Sprintf(format, extra) + } } if i < len(m.Meta[k]) { @@ -1567,11 +1592,11 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{ n += len(arg) - i } - if x, ok := m.Meta[arg[i]]; ok && len(x) == n { - m.Add("option", "args", arg[i]) - continue - } - + // if x, ok := m.Meta[arg[i]]; ok && len(x) == n { + // m.Add("option", "args", arg[i]) + // continue + // } + // m.Add("option", arg[i], arg[i+1:i+1+n]...) i += n } diff --git a/src/contexts/mdb/mdb.go b/src/contexts/mdb/mdb.go index ae21887c..a1a99fb2 100644 --- a/src/contexts/mdb/mdb.go +++ b/src/contexts/mdb/mdb.go @@ -239,7 +239,7 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", "show": &ctx.Command{ Name: "show table fields... [where conditions] [group fields] [order fields] [limit fields] [offset fields] [save filename] [other rest...]", Help: "查询数据库, table: 表名, fields: 字段, where: 查询条件, group: 聚合字段, order: 排序字段", - Form: map[string]int{"where": 1, "group": 1, "order": 1, "limit": 1, "offset": 1, "extras": 1, "save": 1, "export": 1, "other": -1}, + Form: map[string]int{"where": 1, "group": 1, "order": 1, "limit": 1, "offset": 1, "extras": 1, "extra_format": 1, "trans_field": 1, "trans_map": 2, "save": 1, "export": 1, "other": -1}, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if mdb, ok := m.Target().Server.(*MDB); m.Assert(ok) { // {{{ table := m.Confx("table", arg, 0) @@ -281,10 +281,21 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", m.Color(31, table).Echo(" %s %s %s %s %s %v\n", where, group, order, limit, offset, m.Meta["other"]) } - msg.Table(func(maps map[string]string, lists []string, line int) bool { + m.Table(func(maps map[string]string, lists []string, line int) bool { for i, v := range lists { if m.Options("save") { - m.Echo(maps[msg.Meta["append"][i]]) + key := m.Meta["append"][i] + value := maps[key] + if key == m.Option("trans_field") { + for i := 0; i < len(m.Meta["trans_map"])-1; i += 2 { + if value == m.Meta["trans_map"][i] { + value = m.Meta["trans_map"][i+1] + break + } + } + } + + m.Echo(value) } else if line == -1 { m.Color(32, v) } else { @@ -305,7 +316,7 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", table_name := path.Base(m.Option("export")) - msg.Table(func(maps map[string]string, lists []string, line int) bool { + m.Table(func(maps map[string]string, lists []string, line int) bool { if line == -1 { f.WriteString("insert into ") f.WriteString(table_name) @@ -316,7 +327,7 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", if line != -1 { f.WriteString("'") } - f.WriteString(maps[msg.Meta["append"][i]]) + f.WriteString(maps[m.Meta["append"][i]]) if line != -1 { f.WriteString("'") } @@ -384,6 +395,11 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", } // }}} }}, + "demo": &ctx.Command{Name: "get field offset table where [parse func field]", Help: "执行查询语句", + Form: map[string]int{"parse": 1}, + Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Echo("%v", m.Meta) + }}, }, Index: map[string]*ctx.Context{ "void": &ctx.Context{Name: "void",