1
0
forked from x/ContextOS

tce pro mdb

This commit is contained in:
shaoying 2018-06-27 19:23:59 +08:00
parent c2923c9c62
commit 37944d7e67
2 changed files with 77 additions and 60 deletions

View File

@ -1634,9 +1634,13 @@ func (m *Message) Confx(key string, arg ...interface{}) string { // {{{
} }
skip := false skip := false
format := "%s"
if len(arg) > 1 { if len(arg) > 1 {
if v, ok := arg[1].(bool); ok && !v { switch v := arg[1].(type) {
skip = true case bool:
skip = !v
case string:
format = v
} }
} }
@ -1644,9 +1648,12 @@ func (m *Message) Confx(key string, arg ...interface{}) string { // {{{
switch v := arg[0].(type) { switch v := arg[0].(type) {
case string: case string:
if skip || v == "" { if skip || v == "" {
return m.Conf(key) v = m.Conf(key)
} }
if v == "" {
return v return v
}
return fmt.Sprintf(format, v)
case []string: case []string:
which := 0 which := 0
if len(arg) > 1 { if len(arg) > 1 {
@ -1654,10 +1661,19 @@ func (m *Message) Confx(key string, arg ...interface{}) string { // {{{
which = x which = x
} }
} }
value := ""
if which < len(v) { if which < len(v) {
return v[which] value = v[which]
} else {
value = m.Conf(key)
} }
return m.Conf(key) if len(arg) > 2 {
format = arg[2].(string)
}
if value == "" {
return value
}
return fmt.Sprintf(format, value)
default: default:
x := fmt.Sprintf("%v", v) x := fmt.Sprintf("%v", v)
if skip || v == nil || x == "" { if skip || v == nil || x == "" {

View File

@ -17,8 +17,9 @@ import ( // {{{
type MDB struct { type MDB struct {
*sql.DB *sql.DB
table []string
db []string db []string
table []string
*ctx.Context *ctx.Context
} }
@ -37,6 +38,8 @@ func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
"where": &ctx.Config{Name: "条件", Value: "", Help: "条件"}, "where": &ctx.Config{Name: "条件", Value: "", Help: "条件"},
"group": &ctx.Config{Name: "聚合", Value: "", Help: "聚合"}, "group": &ctx.Config{Name: "聚合", Value: "", Help: "聚合"},
"order": &ctx.Config{Name: "排序", Value: "", Help: "排序"}, "order": &ctx.Config{Name: "排序", Value: "", Help: "排序"},
"limit": &ctx.Config{Name: "分页", Value: "", Help: "分页"},
"offset": &ctx.Config{Name: "偏移", Value: "", Help: "偏移"},
"parse": &ctx.Config{Name: "解析", Value: "", Help: "解析"}, "parse": &ctx.Config{Name: "解析", Value: "", Help: "解析"},
} }
@ -242,38 +245,38 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
Form: map[string]int{"where": 1, "group": 1, "order": 1, "limit": 1, "offset": 1, "extras": 1, "save": 1}, Form: map[string]int{"where": 1, "group": 1, "order": 1, "limit": 1, "offset": 1, "extras": 1, "save": 1},
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if mdb, ok := m.Target().Server.(*MDB); m.Assert(ok) { // {{{ if mdb, ok := m.Target().Server.(*MDB); m.Assert(ok) { // {{{
table := arg[0] table := m.Confx("table", arg, 0)
index, e := strconv.Atoi(arg[0]) if i, e := strconv.Atoi(table); e == nil && i < len(mdb.table) {
if e == nil && index < len(mdb.table) { table = mdb.table[i]
table = mdb.table[index]
} }
fields := arg[1:] fields := []string{"*"}
if len(arg) < 2 { if len(arg) > 1 {
fields = []string{"*"} if fields = arg[1:]; m.Options("extras") {
}
if m.Options("extras") {
fields = append(fields, "extra") fields = append(fields, "extra")
} }
} else if m.Confs("field") {
fields = []string{m.Conf("field")}
}
field := strings.Join(fields, ",") field := strings.Join(fields, ",")
where := m.Optionx("where", "where %s") where := m.Confx("where", m.Option("where"), "where %s")
group := m.Optionx("group", "group by %s") group := m.Confx("group", m.Option("group"), "group by %s")
order := m.Optionx("order", "order by %s") order := m.Confx("order", m.Option("order"), "order by %s")
limit := m.Optionx("limit", "limit %s") limit := m.Confx("limit", m.Option("limit"), "limit %s")
offset := m.Optionx("offset", "offset %s") offset := m.Confx("offset", m.Option("offset"), "offset %s")
msg := m.Spawn().Cmd("query", fmt.Sprintf("select %s from %s %s %s %s %s %s", field, table, where, group, order, limit, offset)) msg := m.Spawn().Cmd("query", fmt.Sprintf("select %s from %s %s %s %s %s %s", field, table, where, group, order, limit, offset))
if !m.Options("save") { if !m.Options("save") {
m.Echo("\033[31m%s\033[0m %s %s %s\n", table, where, group, order) m.Color(31, table).Echo(" %s %s %s %s %s\n", where, group, order, limit, offset)
} }
msg.Table(func(maps map[string]string, lists []string, index int) bool { msg.Table(func(maps map[string]string, lists []string, line int) bool {
for i, v := range lists { for i, v := range lists {
if m.Options("save") { if m.Options("save") {
m.Echo(maps[msg.Meta["append"][i]]) m.Echo(maps[msg.Meta["append"][i]])
} else if index == -1 { } else if line == -1 {
m.Echo("\033[32m%s\033[0m", v) m.Color(32, v)
} else { } else {
m.Echo(v) m.Echo(v)
} }
@ -294,31 +297,29 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
f.WriteString(v) f.WriteString(v)
} }
} }
} } // }}}
// }}}
}}, }},
"get": &ctx.Command{Name: "get [where str] [parse str] [table [field]]", Help: "执行查询语句", "get": &ctx.Command{Name: "get field table where offset [parse func field]", Help: "执行查询语句",
Form: map[string]int{"where": 1, "parse": 2}, Form: map[string]int{"parse": 2},
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
where := m.Confx("where") // {{{ field := m.Confx("field", arg, 0) // {{{
if where != "" { table := m.Confx("table", arg, 1, "from %s")
where = "where " + where where := m.Confx("where", arg, 2, "where %s")
} limit := "limit 1"
offset := m.Confx("offset", arg, 3, "offset %s")
msg := m.Spawn().Cmd("query", fmt.Sprintf("select %s %s %s %s %s", field, table, where, limit, offset))
value := m.Append(msg.Meta["append"][0])
parse := m.Confx("parse", m.Option("parse")) parse := m.Confx("parse", m.Option("parse"))
extra := m.Confx("extra", m.Meta["parse"], 1)
table := m.Confx("table", arg, 0)
field := m.Confx("field", arg, 1)
msg := m.Spawn().Cmd("query", fmt.Sprintf("select %s from %s %s", field, table, where))
msg.Table(func(row map[string]string, lists []string, index int) bool {
if index == -1 {
return true
}
data := map[string]interface{}{}
switch parse { switch parse {
case "json": case "json":
if json.Unmarshal([]byte(row[field]), &data); extra == "" { extra := ""
if len(m.Meta["parse"]) > 1 {
extra = m.Meta["parse"][1]
}
data := map[string]interface{}{}
if json.Unmarshal([]byte(value), &data); extra == "" {
for k, v := range data { for k, v := range data {
m.Echo("%s: %v\n", k, v) m.Echo("%s: %v\n", k, v)
} }
@ -326,10 +327,10 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
m.Echo("%v", v) m.Echo("%v", v)
} }
default: default:
m.Echo("%v", row[field]) m.Echo("%v", value)
} }
return false return
}) // }}} // }}}
}}, }},
}, },
Index: map[string]*ctx.Context{ Index: map[string]*ctx.Context{