1
0
forked from x/ContextOS

tce add mdb.get

This commit is contained in:
shaoying 2018-05-13 22:35:26 +08:00
parent 1ad0af36cb
commit 47c4683446
6 changed files with 89 additions and 18 deletions

View File

@ -1,5 +1,3 @@
source etc/local.shy
login root root
config debug on
~ssh
listen ":9090"

View File

@ -102,8 +102,8 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
yac.Cmd("train", "num", "num", "mul{", "[1-9][0-9]*", "0[0-9]+", "0x[0-9]+", "}")
yac.Cmd("train", "str", "str", "mul{", "\"[^\"]*\"", "'[^']*'", "}")
yac.Cmd("train", "tran", "tran", "mul{", "@", "$", "}", "opt{", "$", "[a-zA-Z0-9]+", "}")
yac.Cmd("train", "word", "word", "mul{", "~", "!", "tran", "str", "[a-zA-Z0-9_/.]+", "}")
yac.Cmd("train", "tran", "tran", "mul{", "@", "$", "}", "opt{", "[a-zA-Z0-9]+", "}")
yac.Cmd("train", "word", "word", "mul{", "~", "!", "tran", "str", "[a-zA-Z0-9_/.:]+", "}")
yac.Cmd("train", "op1", "op1", "mul{", "$", "@", "}")
yac.Cmd("train", "op1", "op1", "mul{", "-z", "-n", "}")
@ -196,6 +196,7 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
// }}}
func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
m.Sesss("cli", m)
cli.Caches["#"] = &ctx.Cache{Name: "参数个数", Value: fmt.Sprintf("%d", len(arg)), Help: "参数个数"}
for i, v := range arg {
cli.Caches[fmt.Sprintf("%d", i)] = &ctx.Cache{Name: "执行参数", Value: v, Help: "执行参数"}
@ -229,7 +230,6 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
}
cli.nfs = m.Sesss("nfs", "nfs")
// m.Target().Sessions["nfs"] = cli.nfs
if m.Has("stdio") {
cli.nfs.Cmd("scan", m.Cap("stream", "stdio"), m.Spawn(m.Target()).Cmd("source", m.Cap("init.shy")).Get("result"))
} else {

View File

@ -871,22 +871,28 @@ func (m *Message) Sess(key string, arg ...string) *Message { // {{{
}
// }}}
func (m *Message) Sesss(key string, arg ...string) *Message { // {{{
func (m *Message) Sesss(key string, arg ...interface{}) *Message { // {{{
if _, ok := m.Sessions[key]; !ok && len(arg) > 0 {
switch msg := arg[0].(type) {
case *Message:
m.Sessions[key] = msg
return msg
}
root := true
if len(arg) > 2 {
root = Right(arg[2])
root = Right(arg[2].(string))
}
method := "find"
if len(arg) > 1 {
method = arg[1]
method = arg[1].(string)
}
switch method {
case "find":
m.Sessions[key] = m.Find(arg[0], root)
m.Sessions[key] = m.Find(arg[0].(string), root)
case "search":
m.Sessions[key] = m.Search(arg[0], root)[0]
m.Sessions[key] = m.Search(arg[0].(string), root)[0]
}
return m.Sessions[key]
}
@ -1106,6 +1112,24 @@ func (m *Message) Copy(msg *Message, meta string, arg ...string) *Message { // {
return m
}
// }}}
func (m *Message) Table(cb func(map[string]string) bool) *Message { // {{{
if len(m.Meta["append"]) > 0 {
for i := 0; i < len(m.Meta[m.Meta["append"][0]]); i++ {
row := map[string]string{}
for _, k := range m.Meta["append"] {
if i < len(m.Meta[k]) {
row[k] = m.Meta[k][i]
}
}
if !cb(row) {
break
}
}
}
return m
}
// }}}
func (m *Message) Insert(meta string, index int, arg ...interface{}) string { // {{{

View File

@ -2,6 +2,7 @@ package mdb // {{{
// }}}
import ( // {{{
"contexts"
"encoding/json"
"strconv"
"strings"
@ -28,7 +29,12 @@ func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
"source": &ctx.Cache{Name: "数据库参数", Value: "", Help: "数据库参数"},
"driver": &ctx.Cache{Name: "数据库驱动", Value: "", Help: "数据库驱动"},
}
c.Configs = map[string]*ctx.Config{}
c.Configs = map[string]*ctx.Config{
"table": &ctx.Config{Name: "关系表", Value: "", Help: "关系表"},
"field": &ctx.Config{Name: "字段名", Value: "", Help: "字段名"},
"where": &ctx.Config{Name: "条件", Value: "", Help: "条件"},
"parse": &ctx.Config{Name: "解析", Value: "", Help: "解析"},
}
s := new(MDB)
s.Context = c
@ -206,6 +212,50 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
}
// }}}
}},
"get": &ctx.Command{Name: "get [where str] [parse str] [table [field]]", Help: "执行查询语句",
Formats: map[string]int{"where": 1, "parse": 1},
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
where := m.Conf("where")
if m.Options("where") {
where = m.Option("where")
}
parse := m.Conf("parse")
if m.Options("parse") {
parse = m.Option("parse")
}
table := m.Conf("table")
if len(arg) > 0 {
table = arg[0]
}
field := m.Conf("field")
if len(arg) > 1 {
field = arg[1]
}
rest := []string{}
if len(arg) > 2 {
rest = arg[2:]
}
msg := m.Spawn(m.Target())
msg.Cmd("query", fmt.Sprintf("select %s from %s where %s", field, table, where), rest)
m.Copy(msg, "result").Copy(msg, "append")
m.Table(func(row map[string]string) bool {
data := map[string]interface{}{}
switch parse {
case "json":
json.Unmarshal([]byte(row[field]), data)
m.Echo("%v", data)
default:
m.Echo("%v", row[field])
}
return false
})
}},
"list": &ctx.Command{Name: "list add table field [where condition]", Help: "执行查询语句",
Formats: map[string]int{"where": 1},
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {

View File

@ -85,7 +85,7 @@ func (web *WEB) generate(m *ctx.Message, uri string, arg ...string) string { //
args := []string{}
for i := 0; i < len(arg)-1; i += 2 {
args = append(args, arg[i]+"="+arg[i+1])
args = append(args, arg[i]+"="+url.QueryEscape(arg[i+1]))
}
p := strings.Join(args, "&")

View File

@ -366,14 +366,13 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心",
// }}}
}},
"parse": &ctx.Command{Name: "parse page void word...", Help: "解析语句, page: 语法集合, void: 空白语法集合, word: 语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { // {{{
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok, "模块类型错误") { // {{{
page, ok := yac.page[arg[0]]
m.Assert(ok)
m.Assert(ok, "语法集合错误")
void, ok := yac.page[arg[1]]
m.Assert(ok)
m.Assert(ok, "词法集合错误")
if cli, ok := m.Data["cli"].(*ctx.Context); m.Assert(ok) {
m.Sessions["cli"] = m.Spawn(cli)
if cli, ok := m.Data["cli"].(*ctx.Context); m.Assert(ok, "执行模块错误") {
cli, rest, word := yac.parse(m, cli, page, void, strings.Join(arg[2:], " "))
m.Data["cli"] = cli
m.Result(0, rest, word)