mirror of
https://shylinux.com/x/ContextOS
synced 2025-04-25 16:58:06 +08:00
opt mdb
This commit is contained in:
parent
c0c9f26fe2
commit
013b38fb1b
@ -4,5 +4,5 @@ var version = struct {
|
|||||||
host string
|
host string
|
||||||
self int
|
self int
|
||||||
}{
|
}{
|
||||||
"2019-07-27 06:00:22", "mac", 219,
|
"2019-07-27 12:43:56", "ZYB-20190522USI", 276,
|
||||||
}
|
}
|
||||||
|
@ -1,33 +1,34 @@
|
|||||||
package mdb
|
package mdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"contexts/ctx"
|
|
||||||
"time"
|
|
||||||
"toolkit"
|
|
||||||
|
|
||||||
"database/sql"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/gomodule/redigo/redis"
|
"github.com/gomodule/redigo/redis"
|
||||||
|
|
||||||
|
"contexts/ctx"
|
||||||
|
"toolkit"
|
||||||
|
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MDB struct {
|
type MDB struct {
|
||||||
*sql.DB
|
|
||||||
conn redis.Conn
|
conn redis.Conn
|
||||||
|
*sql.DB
|
||||||
*ctx.Context
|
*ctx.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||||
c.Caches = map[string]*ctx.Cache{
|
c.Caches = map[string]*ctx.Cache{
|
||||||
"database": &ctx.Cache{Name: "database", Value: m.Confx("database", arg, 0), Help: "数据库"},
|
"database": &ctx.Cache{Name: "database", Value: m.Confx("database", arg, 0), Help: "数据库"},
|
||||||
"username": &ctx.Cache{Name: "username", Value: m.Confx("username", arg, 1), Help: "账户"},
|
"username": &ctx.Cache{Name: "username", Value: m.Confx("username", arg, 1), Help: "用户名"},
|
||||||
"password": &ctx.Cache{Name: "password", Value: m.Confx("password", arg, 2), Help: "密码"},
|
"password": &ctx.Cache{Name: "password", Value: m.Confx("password", arg, 2), Help: "密码"},
|
||||||
"address": &ctx.Cache{Name: "address", Value: m.Confx("address", arg, 3), Help: "地址"},
|
"address": &ctx.Cache{Name: "address", Value: m.Confx("address", arg, 3), Help: "地址"},
|
||||||
"protocol": &ctx.Cache{Name: "protocol(tcp)", Value: m.Confx("protocol", arg, 4), Help: "协议"},
|
"protocol": &ctx.Cache{Name: "protocol(tcp)", Value: m.Confx("protocol", arg, 4), Help: "协议"},
|
||||||
"driver": &ctx.Cache{Name: "driver(mysql)", Value: m.Confx("driver", arg, 5), Help: "驱动"},
|
"driver": &ctx.Cache{Name: "driver(mysql)", Value: m.Confx("driver", arg, 5), Help: "驱动"},
|
||||||
|
"redis": &ctx.Cache{Name: "redis", Value: "", Help: "数据缓存"},
|
||||||
}
|
}
|
||||||
c.Configs = map[string]*ctx.Config{
|
c.Configs = map[string]*ctx.Config{
|
||||||
"dbs": &ctx.Config{Name: "dbs", Value: []string{}, Help: "数据库"},
|
"dbs": &ctx.Config{Name: "dbs", Value: []string{}, Help: "数据库"},
|
||||||
@ -36,18 +37,17 @@ func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
|||||||
"offset": &ctx.Config{Name: "offset", Value: "0", Help: "偏移"},
|
"offset": &ctx.Config{Name: "offset", Value: "0", Help: "偏移"},
|
||||||
}
|
}
|
||||||
|
|
||||||
s := new(MDB)
|
return &MDB{Context: c}
|
||||||
s.Context = c
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
func (mdb *MDB) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
func (mdb *MDB) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||||
return mdb
|
return mdb
|
||||||
}
|
}
|
||||||
func (mdb *MDB) Start(m *ctx.Message, arg ...string) bool {
|
func (mdb *MDB) Start(m *ctx.Message, arg ...string) bool {
|
||||||
db, e := sql.Open(m.Cap("driver"), fmt.Sprintf("%s:%s@%s(%s)/%s", m.Cap("username"), m.Cap("password"), m.Cap("protocol"), m.Cap("address"), m.Cap("database")))
|
if db, e := sql.Open(m.Cap("driver"), fmt.Sprintf("%s:%s@%s(%s)/%s", m.Cap("username"), m.Cap("password"),
|
||||||
m.Assert(e)
|
m.Cap("protocol"), m.Cap("address"), m.Cap("database"))); m.Assert(e) {
|
||||||
mdb.DB = db
|
m.Log("info", "mdb open %s", m.Cap("stream", m.Cap("database")))
|
||||||
m.Log("info", "mdb open %s", m.Cap("stream", m.Cap("database")))
|
mdb.DB = db
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
func (mdb *MDB) Close(m *ctx.Message, arg ...string) bool {
|
func (mdb *MDB) Close(m *ctx.Message, arg ...string) bool {
|
||||||
@ -57,16 +57,19 @@ func (mdb *MDB) Close(m *ctx.Message, arg ...string) bool {
|
|||||||
var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
||||||
Caches: map[string]*ctx.Cache{
|
Caches: map[string]*ctx.Cache{
|
||||||
"nsource": &ctx.Cache{Name: "nsource", Value: "0", Help: "已打开数据库的数量"},
|
"nsource": &ctx.Cache{Name: "nsource", Value: "0", Help: "已打开数据库的数量"},
|
||||||
"redis": &ctx.Cache{Name: "redis", Value: "", Help: "服务地址"},
|
|
||||||
},
|
},
|
||||||
Configs: map[string]*ctx.Config{
|
Configs: map[string]*ctx.Config{
|
||||||
"database": &ctx.Config{Name: "database", Value: "demo", Help: "默认数据库"},
|
"database": &ctx.Config{Name: "database", Value: "demo", Help: "默认数据库"},
|
||||||
"username": &ctx.Config{Name: "username", Value: "demo", Help: "默认账户"},
|
"username": &ctx.Config{Name: "username", Value: "demo", Help: "默认账户"},
|
||||||
"password": &ctx.Config{Name: "password", Value: "demo", Help: "默认密码"},
|
"password": &ctx.Config{Name: "password", Value: "demo", Help: "默认密码"},
|
||||||
|
"address": &ctx.Config{Name: "address", Value: ":6379", Help: "默认地址"},
|
||||||
"protocol": &ctx.Config{Name: "protocol(tcp)", Value: "tcp", Help: "默认协议"},
|
"protocol": &ctx.Config{Name: "protocol(tcp)", Value: "tcp", Help: "默认协议"},
|
||||||
"address": &ctx.Config{Name: "address", Value: "", Help: "默认地址"},
|
|
||||||
"driver": &ctx.Config{Name: "driver(mysql)", Value: "mysql", Help: "默认驱动"},
|
"driver": &ctx.Config{Name: "driver(mysql)", Value: "mysql", Help: "默认驱动"},
|
||||||
|
|
||||||
|
"ktv": &ctx.Config{Name: "ktv", Value: map[string]interface{}{
|
||||||
|
"conf": map[string]interface{}{"expire": "24h"}, "data": map[string]interface{}{},
|
||||||
|
}, Help: "缓存数据"},
|
||||||
|
|
||||||
"temp": &ctx.Config{Name: "temp", Value: map[string]interface{}{}, Help: "缓存数据"},
|
"temp": &ctx.Config{Name: "temp", Value: map[string]interface{}{}, Help: "缓存数据"},
|
||||||
"temp_view": &ctx.Config{Name: "temp_view", Value: map[string]interface{}{}, Help: "缓存数据"},
|
"temp_view": &ctx.Config{Name: "temp_view", Value: map[string]interface{}{}, Help: "缓存数据"},
|
||||||
"temp_expire": &ctx.Config{Name: "temp_expire(s)", Value: "3000", Help: "缓存数据"},
|
"temp_expire": &ctx.Config{Name: "temp_expire(s)", Value: "3000", Help: "缓存数据"},
|
||||||
@ -96,102 +99,11 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
"base": []interface{}{"key", "create_time", "type", "name", "model", "value"},
|
"base": []interface{}{"key", "create_time", "type", "name", "model", "value"},
|
||||||
"full": []interface{}{"key", "create_time", "access_time", "type", "name", "model", "value", "view", "data", "ship"},
|
"full": []interface{}{"key", "create_time", "access_time", "type", "name", "model", "value", "view", "data", "ship"},
|
||||||
}, Help: "数据视图"},
|
}, Help: "数据视图"},
|
||||||
|
|
||||||
"ktv": &ctx.Config{Name: "ktv", Value: map[string]interface{}{
|
|
||||||
"conf": map[string]interface{}{"expire": "24h"}, "data": map[string]interface{}{},
|
|
||||||
}, Help: "缓存数据"},
|
|
||||||
},
|
},
|
||||||
Commands: map[string]*ctx.Command{
|
Commands: map[string]*ctx.Command{
|
||||||
"ktv": &ctx.Command{Name: "ktv", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
|
||||||
if len(arg) == 0 {
|
|
||||||
now := kit.Int(m.Time("stamp"))
|
|
||||||
m.Confm("ktv", "data", func(key string, value map[string]interface{}) {
|
|
||||||
m.Push("key", key)
|
|
||||||
m.Push("expire", kit.Int(value["expire"])-now)
|
|
||||||
m.Push("value", value["value"])
|
|
||||||
})
|
|
||||||
m.Table()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(arg) == 1 {
|
|
||||||
if m.Confi("ktv", []string{"data", arg[0], "expire"}) < kit.Int(m.Time("stamp")) {
|
|
||||||
m.Conf("ktv", []string{"data", arg[0]}, "")
|
|
||||||
}
|
|
||||||
m.Echo(m.Conf("ktv", []string{"data", arg[0], "value"}))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
m.Confv("ktv", []string{"data", arg[0]}, map[string]interface{}{
|
|
||||||
"expire": m.Time(kit.Select(m.Conf("ktv", "conf.expire"), arg, 2), "stamp"),
|
|
||||||
"value": arg[1],
|
|
||||||
})
|
|
||||||
m.Echo(arg[1])
|
|
||||||
return
|
|
||||||
}},
|
|
||||||
"redis": &ctx.Command{Name: "redis open address [protocol]", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
|
||||||
if mdb, ok := m.Target().Server.(*MDB); m.Assert(ok) {
|
|
||||||
switch arg[0] {
|
|
||||||
case "open":
|
|
||||||
mdb.conn, e = redis.Dial("tcp", m.Cap("redis", arg[1]), redis.DialKeepAlive(time.Second*10))
|
|
||||||
default:
|
|
||||||
if mdb.conn == nil {
|
|
||||||
m.Echo("not open")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if mdb.conn.Err() != nil {
|
|
||||||
mdb.conn, e = redis.Dial("tcp", m.Cap("redis"), redis.DialKeepAlive(time.Second*10))
|
|
||||||
}
|
|
||||||
args := []interface{}{}
|
|
||||||
for _, v := range arg[1:] {
|
|
||||||
args = append(args, v)
|
|
||||||
}
|
|
||||||
res, err := mdb.conn.Do(arg[0], args...)
|
|
||||||
m.Assert(err)
|
|
||||||
switch val := res.(type) {
|
|
||||||
case redis.Error:
|
|
||||||
m.Echo("%v", val)
|
|
||||||
case []interface{}:
|
|
||||||
for i, v := range val {
|
|
||||||
m.Add("append", "index", i)
|
|
||||||
m.Add("append", "value", v)
|
|
||||||
}
|
|
||||||
m.Table()
|
|
||||||
default:
|
|
||||||
str := kit.Format(res)
|
|
||||||
var data interface{}
|
|
||||||
if json.Unmarshal([]byte(str), &data) == nil {
|
|
||||||
m.Echo(kit.Formats(data))
|
|
||||||
} else {
|
|
||||||
m.Echo(str)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}},
|
|
||||||
"open": &ctx.Command{Name: "open [database [username [password [address [protocol [driver]]]]]]",
|
"open": &ctx.Command{Name: "open [database [username [password [address [protocol [driver]]]]]]",
|
||||||
Help: "打开数据库, database: 数据库名, username: 用户名, password: 密码, address: 服务地址, protocol: 服务协议, driver: 数据库类型",
|
Help: "打开数据库, database: 数据库名, username: 用户名, password: 密码, address: 服务地址, protocol: 服务协议, driver: 数据库类型",
|
||||||
Form: map[string]int{"dbname": 1, "dbhelp": 1},
|
Form: map[string]int{"dbname": 1, "dbhelp": 1}, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
||||||
Auto: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) bool {
|
|
||||||
switch len(arg) {
|
|
||||||
case 0:
|
|
||||||
m.Auto("", fmt.Sprintf("database(%s)", m.Conf("database")), "数据库")
|
|
||||||
case 1:
|
|
||||||
m.Auto("", fmt.Sprintf("username(%s)", m.Conf("username")), "账号")
|
|
||||||
case 2:
|
|
||||||
m.Auto("", fmt.Sprintf("password(%s)", m.Conf("password")), "密码")
|
|
||||||
case 3:
|
|
||||||
m.Auto("", fmt.Sprintf("address(%s)", m.Conf("address")), "地址")
|
|
||||||
case 4:
|
|
||||||
m.Auto("", fmt.Sprintf("protocol(%s)", m.Conf("protocol")), "协议")
|
|
||||||
case 5:
|
|
||||||
m.Auto("", fmt.Sprintf("driver(%s)", m.Conf("driver")), "驱动")
|
|
||||||
default:
|
|
||||||
m.Auto("", "[dbname name]", "模块名")
|
|
||||||
m.Auto("", "[dbhelp help]", "帮助文档")
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
},
|
|
||||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
|
||||||
m.Start(kit.Select(fmt.Sprintf("db%d", m.Capi("nsource", 1)), m.Option("dbname")),
|
m.Start(kit.Select(fmt.Sprintf("db%d", m.Capi("nsource", 1)), m.Option("dbname")),
|
||||||
kit.Select("数据源", m.Option("dbhelp")), arg...)
|
kit.Select("数据源", m.Option("dbhelp")), arg...)
|
||||||
return
|
return
|
||||||
@ -203,15 +115,12 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
which = append(which, v)
|
which = append(which, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
ret, e := mdb.Exec(arg[0], which...)
|
if ret, e := mdb.Exec(arg[0], which...); m.Assert(e) {
|
||||||
m.Assert(e)
|
id, _ := ret.LastInsertId()
|
||||||
id, e := ret.LastInsertId()
|
n, _ := ret.RowsAffected()
|
||||||
m.Assert(e)
|
m.Log("info", "last(%s) nrow(%s)", m.Append("last", id), m.Append("nrow", n))
|
||||||
n, e := ret.RowsAffected()
|
m.Echo("%d", n)
|
||||||
m.Assert(e)
|
}
|
||||||
|
|
||||||
m.Log("info", "last(%s) nrow(%s)", m.Append("last", id), m.Append("nrow", n))
|
|
||||||
m.Echo("%d", n)
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}},
|
}},
|
||||||
@ -222,120 +131,108 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
which = append(which, v)
|
which = append(which, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, e := mdb.Query(arg[0], which...)
|
if rows, e := mdb.Query(arg[0], which...); m.Assert(e) {
|
||||||
m.Assert(e)
|
defer rows.Close()
|
||||||
defer rows.Close()
|
if cols, e := rows.Columns(); m.Assert(e) {
|
||||||
|
num := len(cols)
|
||||||
|
|
||||||
cols, e := rows.Columns()
|
for rows.Next() {
|
||||||
m.Assert(e)
|
vals := make([]interface{}, num)
|
||||||
num := len(cols)
|
ptrs := make([]interface{}, num)
|
||||||
|
for i := range vals {
|
||||||
|
ptrs[i] = &vals[i]
|
||||||
|
}
|
||||||
|
rows.Scan(ptrs...)
|
||||||
|
|
||||||
for rows.Next() {
|
for i, k := range cols {
|
||||||
vals := make([]interface{}, num)
|
m.Push(k, vals[i])
|
||||||
ptrs := make([]interface{}, num)
|
}
|
||||||
for i := range vals {
|
}
|
||||||
ptrs[i] = &vals[i]
|
|
||||||
|
if len(m.Meta["append"]) > 0 {
|
||||||
|
m.Log("info", "rows(%d) cols(%d)", len(m.Meta[m.Meta["append"][0]]), len(m.Meta["append"]))
|
||||||
|
} else {
|
||||||
|
m.Log("info", "rows(0) cols(0)")
|
||||||
|
}
|
||||||
|
m.Table()
|
||||||
}
|
}
|
||||||
rows.Scan(ptrs...)
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}},
|
||||||
|
"redis": &ctx.Command{Name: "redis [open address]|[cmd...]", Help: "缓存数据库", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
||||||
|
if mdb, ok := m.Target().Server.(*MDB); m.Assert(ok) {
|
||||||
|
switch arg[0] {
|
||||||
|
case "open":
|
||||||
|
if mdb.conn, e = redis.Dial("tcp", m.Cap("redis", arg[1]), redis.DialKeepAlive(time.Second*10)); m.Assert(e) {
|
||||||
|
m.Log("info", "redis: %v", arg[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
if mdb.conn == nil || mdb.conn.Err() != nil {
|
||||||
|
if m.Caps("redis") {
|
||||||
|
m.Cmd("mdb.redis", "open", m.Cap("redis"))
|
||||||
|
} else {
|
||||||
|
m.Echo("not open")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []interface{}{}
|
||||||
|
for _, v := range arg[1:] {
|
||||||
|
args = append(args, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if res, err := mdb.conn.Do(arg[0], args...); m.Assert(err) {
|
||||||
|
switch val := res.(type) {
|
||||||
|
case redis.Error:
|
||||||
|
m.Echo("%v", val)
|
||||||
|
|
||||||
|
case []interface{}:
|
||||||
|
for i, v := range val {
|
||||||
|
m.Add("append", "index", i)
|
||||||
|
m.Add("append", "value", v)
|
||||||
|
}
|
||||||
|
m.Table()
|
||||||
|
|
||||||
for i, k := range cols {
|
|
||||||
switch b := vals[i].(type) {
|
|
||||||
case nil:
|
|
||||||
m.Add("append", k, "")
|
|
||||||
case []byte:
|
|
||||||
m.Add("append", k, string(b))
|
|
||||||
case int64:
|
|
||||||
m.Add("append", k, fmt.Sprintf("%d", b))
|
|
||||||
default:
|
default:
|
||||||
m.Add("append", k, fmt.Sprintf("%v", b))
|
var data interface{}
|
||||||
|
if str := kit.Format(res); json.Unmarshal([]byte(str), &data) == nil {
|
||||||
|
m.Echo(kit.Formats(data))
|
||||||
|
} else {
|
||||||
|
m.Echo(str)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(m.Meta["append"]) > 0 {
|
|
||||||
m.Log("info", "rows(%d) cols(%d)", len(m.Meta[m.Meta["append"][0]]), len(m.Meta["append"]))
|
|
||||||
} else {
|
|
||||||
m.Log("info", "rows(0) cols(0)")
|
|
||||||
}
|
|
||||||
m.Table()
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}},
|
}},
|
||||||
|
|
||||||
"db": &ctx.Command{Name: "db [which]", Help: "查看或选择数据库",
|
"db": &ctx.Command{Name: "db [which]", Help: "查看或选择数据库", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
||||||
Auto: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) bool {
|
if len(arg) == 0 {
|
||||||
if len(arg) == 0 {
|
m.Cmdy(".query", "show databases").Table()
|
||||||
m.Put("option", "bio.cmd", "").Spawn().Cmd("query", "show databases").Table(func(line map[string]string) {
|
return
|
||||||
for _, v := range line {
|
}
|
||||||
m.Auto(v, "", "")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
},
|
|
||||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
|
||||||
if len(arg) == 0 {
|
|
||||||
m.Cmdy(".query", "show databases").Table()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
m.Assert(m.Cmdy("exec", fmt.Sprintf("use %s", arg[0])))
|
m.Assert(m.Cmdy("exec", fmt.Sprintf("use %s", arg[0])))
|
||||||
m.Echo(m.Cap("database", arg[0]))
|
m.Echo(m.Cap("database", arg[0]))
|
||||||
return
|
return
|
||||||
}},
|
}},
|
||||||
"tab": &ctx.Command{Name: "tab [which [field]]", Help: "查看关系表,which: 表名, field: 字段名",
|
"tab": &ctx.Command{Name: "tab [which [field]]", Help: "查看关系表,which: 表名, field: 字段名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
||||||
Auto: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) bool {
|
switch len(arg) {
|
||||||
switch len(arg) {
|
case 0:
|
||||||
case 0:
|
m.Cmdy(".query", "show tables").Table()
|
||||||
m.Put("option", "bio.cmd", "").Spawn().Cmd("query", "show tables").Table(func(line map[string]string) {
|
case 1:
|
||||||
for _, v := range line {
|
m.Cmdy(".query", fmt.Sprintf("desc %s", arg[0])).Table()
|
||||||
m.Auto(v, "", "")
|
case 2:
|
||||||
}
|
m.Cmdy(".query", fmt.Sprintf("desc %s", arg[0])).Cmd("select", "Field", arg[1]).CopyTo(m)
|
||||||
})
|
}
|
||||||
case 1:
|
return
|
||||||
m.Put("option", "bio.cmd", "").Spawn().Cmd("query", fmt.Sprintf("desc %s", arg[0])).Table(func(line map[string]string) {
|
}},
|
||||||
m.Auto(line["Field"], line["Type"], line["Default"])
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
},
|
|
||||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
|
||||||
switch len(arg) {
|
|
||||||
case 0:
|
|
||||||
m.Cmdy(".query", "show tables").Table()
|
|
||||||
case 1:
|
|
||||||
m.Cmdy(".query", fmt.Sprintf("desc %s", arg[0])).Table()
|
|
||||||
case 2:
|
|
||||||
m.Cmdy(".query", fmt.Sprintf("desc %s", arg[0])).Cmd("select", "Field", arg[1]).CopyTo(m)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}},
|
|
||||||
"show": &ctx.Command{Name: "show table fields...", Help: "查询数据, table: 表名, fields: 字段, where: 查询条件, group: 聚合字段, order: 排序字段",
|
"show": &ctx.Command{Name: "show table fields...", Help: "查询数据, table: 表名, fields: 字段, where: 查询条件, group: 聚合字段, order: 排序字段",
|
||||||
Form: map[string]int{"where": 1, "eq": 2, "like": 2, "in": 2, "begin": 2, "group": 1, "order": 1, "desc": 0, "limit": 1, "offset": 1, "other": -1},
|
Form: map[string]int{"where": 1, "eq": 2, "in": 2, "like": 2, "begin": 2, "group": 1, "order": 1, "desc": 0, "limit": 1, "offset": 1, "other": -1},
|
||||||
Auto: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) bool {
|
|
||||||
if len(arg) == 0 {
|
|
||||||
m.Put("option", "bio.cmd", "").Spawn().Cmd("query", "show tables").Table(func(line map[string]string) {
|
|
||||||
for _, v := range line {
|
|
||||||
m.Auto(v, "", "")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
m.Auto("where", "stmt", "条件语句")
|
|
||||||
m.Auto("eq", "field value", "条件语句")
|
|
||||||
m.Auto("in", "field value", "条件语句")
|
|
||||||
m.Auto("like", "field value", "条件语句")
|
|
||||||
m.Auto("begin", "field value", "条件语句")
|
|
||||||
|
|
||||||
m.Auto("group", "field", "聚合")
|
|
||||||
m.Auto("order", "field", "排序")
|
|
||||||
m.Auto("desc", "", "降序")
|
|
||||||
m.Auto("limit", "10", "分页")
|
|
||||||
m.Auto("offset", "0", "偏移")
|
|
||||||
m.Auto("other", "stmt", "其它")
|
|
||||||
return true
|
|
||||||
},
|
|
||||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
||||||
if len(arg) == 0 {
|
if len(arg) == 0 {
|
||||||
m.Cmdy(".query", "show tables")
|
m.Cmdy(".query", "show tables")
|
||||||
@ -407,172 +304,198 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
values = append(values, fmt.Sprintf("%s='%s'", arg[i], arg[i+1]))
|
values = append(values, fmt.Sprintf("%s='%s'", arg[i], arg[i+1]))
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt := []string{fmt.Sprintf("update %s", arg[0])}
|
stmt := []string{"update", arg[0]}
|
||||||
stmt = append(stmt, "set", strings.Join(values, ","))
|
stmt = append(stmt, "set", strings.Join(values, ","))
|
||||||
stmt = append(stmt, fmt.Sprintf("where %s", arg[1]))
|
stmt = append(stmt, "where", arg[1])
|
||||||
|
|
||||||
m.Cmd(".exec", strings.Join(stmt, " "))
|
m.Cmd(".exec", strings.Join(stmt, " "))
|
||||||
m.Cmdy("show", arg[0], fields, "where", arg[1])
|
|
||||||
|
m.Cmdy(".show", arg[0], fields, "where", arg[1])
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}},
|
}},
|
||||||
|
|
||||||
"temp": &ctx.Command{Name: "temp [type [meta [data]]] [tid [node|ship|data] [chain... [select ...]]]", Form: map[string]int{"select": -1, "limit": 1}, Help: "缓存数据", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
"ktv": &ctx.Command{Name: "ktv", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
||||||
if len(arg) > 0 && arg[0] == "check" {
|
if len(arg) == 0 {
|
||||||
h := ""
|
now := kit.Int(m.Time("stamp"))
|
||||||
for i := 1; i < len(arg)-1; i += 2 {
|
m.Confm("ktv", "data", func(key string, value map[string]interface{}) {
|
||||||
switch arg[i] {
|
m.Push("key", key)
|
||||||
case "url", "trans":
|
m.Push("expire", kit.Int(value["expire"])-now)
|
||||||
h = ""
|
m.Push("value", value["value"])
|
||||||
}
|
})
|
||||||
if h = m.Cmdx("aaa.hash", arg[i], arg[i+1], h); !m.Confs("temp", h) {
|
m.Table()
|
||||||
return
|
|
||||||
}
|
|
||||||
expire := kit.Time(m.Conf("temp", []string{h, "create_time"})) + kit.Int(m.Confx("temp_expire")) - kit.Time(m.Time())
|
|
||||||
m.Log("info", "expire: %ds", expire)
|
|
||||||
if expire < 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.Echo(h)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if len(arg) == 1 {
|
||||||
if len(arg) > 2 { // 添加数据
|
if m.Confi("ktv", []string{"data", arg[0], "expire"}) < kit.Int(m.Time("stamp")) {
|
||||||
if temp := m.Confm("temp", arg[0]); temp == nil {
|
m.Conf("ktv", []string{"data", arg[0]}, "")
|
||||||
h := m.Cmdx("aaa.hash", arg[0], arg[1])
|
|
||||||
m.Confv("temp", h, map[string]interface{}{
|
|
||||||
"create_time": m.Time(), "expire_time": m.Time("60s"),
|
|
||||||
"type": arg[0], "meta": arg[1], "data": m.Optionv(arg[2]),
|
|
||||||
})
|
|
||||||
arg[2], arg = h, arg[2:]
|
|
||||||
}
|
}
|
||||||
|
m.Echo(m.Conf("ktv", []string{"data", arg[0], "value"}))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
m.Confv("ktv", []string{"data", arg[0]}, map[string]interface{}{
|
||||||
if len(arg) > 1 {
|
"expire": m.Time(kit.Select(m.Conf("ktv", "conf.expire"), arg, 2), "stamp"),
|
||||||
if temp, msg := m.Confm("temp", arg[0]), m; temp != nil {
|
"value": arg[1],
|
||||||
hash, arg := arg[0], arg[1:]
|
})
|
||||||
switch arg[0] {
|
m.Echo(arg[1])
|
||||||
case "node": // 查看节点
|
return
|
||||||
m.Put("option", "temp", temp).Cmdy("ctx.trans", "temp")
|
}},
|
||||||
return
|
"temp": &ctx.Command{Name: "temp [type [meta [data]]] [tid [node|ship|data] [chain... [select ...]]]",
|
||||||
case "ship": //查看链接
|
Form: map[string]int{"select": -1, "limit": 1}, Help: "缓存数据", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
||||||
for k, v := range temp {
|
if len(arg) > 0 && arg[0] == "check" {
|
||||||
val := v.(map[string]interface{})
|
h := ""
|
||||||
m.Add("append", "key", k)
|
for i := 1; i < len(arg)-1; i += 2 {
|
||||||
m.Add("append", "create_time", val["create_time"])
|
switch arg[i] {
|
||||||
m.Add("append", "type", val["type"])
|
case "url", "trans":
|
||||||
m.Add("append", "meta", val["meta"])
|
h = ""
|
||||||
}
|
}
|
||||||
m.Sort("create_time", "time_r").Table()
|
if h = m.Cmdx("aaa.hash", arg[i], arg[i+1], h); !m.Confs("temp", h) {
|
||||||
return
|
return
|
||||||
case "data": // 查看数据
|
}
|
||||||
arg = arg[1:]
|
expire := kit.Time(m.Conf("temp", []string{h, "create_time"})) + kit.Int(m.Confx("temp_expire")) - kit.Time(m.Time())
|
||||||
}
|
m.Log("info", "expire: %ds", expire)
|
||||||
|
if expire < 0 {
|
||||||
trans := strings.Join(append([]string{hash, "data"}, arg...), ".")
|
return
|
||||||
h := m.Cmdx("aaa.hash", "trans", trans)
|
|
||||||
|
|
||||||
if len(arg) == 0 || temp["type"].(string) == "trans" {
|
|
||||||
h = hash
|
|
||||||
} else { // 转换数据
|
|
||||||
if view := m.Confm("temp", h); view != nil && false { // 加载缓存
|
|
||||||
msg = m.Spawn()
|
|
||||||
switch data := view["data"].(type) {
|
|
||||||
case map[string][]string:
|
|
||||||
msg.Meta = data
|
|
||||||
case map[string]interface{}:
|
|
||||||
for k, v := range data {
|
|
||||||
switch val := v.(type) {
|
|
||||||
case []interface{}:
|
|
||||||
msg.Add("append", k, val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.Confv("temp", []string{h, "data"}, msg.Meta)
|
|
||||||
}
|
|
||||||
temp = view
|
|
||||||
} else if arg[0] == "hash" { // 添加缓存
|
|
||||||
m.Echo(hash)
|
|
||||||
} else if arg[0] == "" { // 添加缓存
|
|
||||||
b, _ := json.MarshalIndent(temp["data"], "", " ")
|
|
||||||
m.Echo(string(b))
|
|
||||||
} else {
|
|
||||||
msg = m.Put("option", "temp", temp["data"]).Cmd("ctx.trans", "temp", arg)
|
|
||||||
|
|
||||||
m.Confv("temp", h, map[string]interface{}{
|
|
||||||
"create_time": m.Time(), "expire_time": m.Time("60s"),
|
|
||||||
"type": "trans", "meta": trans, "data": msg.Meta,
|
|
||||||
"ship": map[string]interface{}{
|
|
||||||
hash: map[string]interface{}{"create_time": m.Time(), "ship": "0", "type": temp["type"], "meta": temp["meta"]},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
m.Confv("temp", []string{hash, "ship", h}, map[string]interface{}{
|
|
||||||
"create_time": m.Time(), "ship": "1", "type": "trans", "meta": trans,
|
|
||||||
})
|
|
||||||
temp = m.Confm("temp", h)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.Options("select") { // 过滤数据
|
|
||||||
chain := strings.Join(m.Optionv("select").([]string), " ")
|
|
||||||
hh := m.Cmdx("aaa.hash", "select", chain, h)
|
|
||||||
|
|
||||||
if view := m.Confm("temp", hh); view != nil && false { // 加载缓存
|
|
||||||
msg = msg.Spawn()
|
|
||||||
switch data := view["data"].(type) {
|
|
||||||
case map[string][]string:
|
|
||||||
msg.Meta = data
|
|
||||||
case map[string]interface{}:
|
|
||||||
for k, v := range data {
|
|
||||||
switch val := v.(type) {
|
|
||||||
case []interface{}:
|
|
||||||
msg.Add("append", k, val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.Confv("temp", []string{h, "data"}, msg.Meta)
|
|
||||||
}
|
|
||||||
} else { // 添加缓存
|
|
||||||
msg = msg.Spawn().Copy(msg, "append").Cmd("select", m.Optionv("select"))
|
|
||||||
|
|
||||||
m.Confv("temp", hh, map[string]interface{}{
|
|
||||||
"create_time": m.Time(), "expire_time": m.Time("60s"),
|
|
||||||
"type": "select", "meta": chain, "data": msg.Meta,
|
|
||||||
"ship": map[string]interface{}{
|
|
||||||
h: map[string]interface{}{"create_time": m.Time(), "ship": "0", "type": temp["type"], "meta": temp["meta"]},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
m.Confv("temp", []string{h, "ship", hh}, map[string]interface{}{
|
|
||||||
"create_time": m.Time(), "ship": "1", "type": "select", "meta": chain,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.CopyTo(m)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
arg, h := kit.Slice(arg)
|
|
||||||
if h != "" {
|
|
||||||
if temp := m.Confm("temp", h); temp != nil {
|
|
||||||
m.Echo(h)
|
m.Echo(h)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 缓存列表
|
if len(arg) > 2 { // 添加数据
|
||||||
m.Confm("temp", func(key string, temp map[string]interface{}) {
|
if temp := m.Confm("temp", arg[0]); temp == nil {
|
||||||
if len(arg) == 0 || strings.HasPrefix(key, arg[0]) || strings.HasSuffix(key, arg[0]) || (temp["type"].(string) == arg[0] && (len(arg) == 1 || strings.Contains(temp["meta"].(string), arg[1]))) {
|
h := m.Cmdx("aaa.hash", arg[0], arg[1])
|
||||||
m.Add("append", "key", key)
|
m.Confv("temp", h, map[string]interface{}{
|
||||||
m.Add("append", "create_time", temp["create_time"])
|
"create_time": m.Time(), "expire_time": m.Time("60s"),
|
||||||
m.Add("append", "type", temp["type"])
|
"type": arg[0], "meta": arg[1], "data": m.Optionv(arg[2]),
|
||||||
m.Add("append", "meta", temp["meta"])
|
})
|
||||||
|
arg[2], arg = h, arg[2:]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
m.Sort("create_time", "time_r").Table().Cmd("select", m.Optionv("select"))
|
if len(arg) > 1 {
|
||||||
return
|
if temp, msg := m.Confm("temp", arg[0]), m; temp != nil {
|
||||||
}},
|
hash, arg := arg[0], arg[1:]
|
||||||
|
switch arg[0] {
|
||||||
|
case "node": // 查看节点
|
||||||
|
m.Put("option", "temp", temp).Cmdy("ctx.trans", "temp")
|
||||||
|
return
|
||||||
|
case "ship": //查看链接
|
||||||
|
for k, v := range temp {
|
||||||
|
val := v.(map[string]interface{})
|
||||||
|
m.Add("append", "key", k)
|
||||||
|
m.Add("append", "create_time", val["create_time"])
|
||||||
|
m.Add("append", "type", val["type"])
|
||||||
|
m.Add("append", "meta", val["meta"])
|
||||||
|
}
|
||||||
|
m.Sort("create_time", "time_r").Table()
|
||||||
|
return
|
||||||
|
case "data": // 查看数据
|
||||||
|
arg = arg[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
trans := strings.Join(append([]string{hash, "data"}, arg...), ".")
|
||||||
|
h := m.Cmdx("aaa.hash", "trans", trans)
|
||||||
|
|
||||||
|
if len(arg) == 0 || temp["type"].(string) == "trans" {
|
||||||
|
h = hash
|
||||||
|
} else { // 转换数据
|
||||||
|
if view := m.Confm("temp", h); view != nil && false { // 加载缓存
|
||||||
|
msg = m.Spawn()
|
||||||
|
switch data := view["data"].(type) {
|
||||||
|
case map[string][]string:
|
||||||
|
msg.Meta = data
|
||||||
|
case map[string]interface{}:
|
||||||
|
for k, v := range data {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case []interface{}:
|
||||||
|
msg.Add("append", k, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m.Confv("temp", []string{h, "data"}, msg.Meta)
|
||||||
|
}
|
||||||
|
temp = view
|
||||||
|
} else if arg[0] == "hash" { // 添加缓存
|
||||||
|
m.Echo(hash)
|
||||||
|
} else if arg[0] == "" { // 添加缓存
|
||||||
|
b, _ := json.MarshalIndent(temp["data"], "", " ")
|
||||||
|
m.Echo(string(b))
|
||||||
|
} else {
|
||||||
|
msg = m.Put("option", "temp", temp["data"]).Cmd("ctx.trans", "temp", arg)
|
||||||
|
|
||||||
|
m.Confv("temp", h, map[string]interface{}{
|
||||||
|
"create_time": m.Time(), "expire_time": m.Time("60s"),
|
||||||
|
"type": "trans", "meta": trans, "data": msg.Meta,
|
||||||
|
"ship": map[string]interface{}{
|
||||||
|
hash: map[string]interface{}{"create_time": m.Time(), "ship": "0", "type": temp["type"], "meta": temp["meta"]},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
m.Confv("temp", []string{hash, "ship", h}, map[string]interface{}{
|
||||||
|
"create_time": m.Time(), "ship": "1", "type": "trans", "meta": trans,
|
||||||
|
})
|
||||||
|
temp = m.Confm("temp", h)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.Options("select") { // 过滤数据
|
||||||
|
chain := strings.Join(m.Optionv("select").([]string), " ")
|
||||||
|
hh := m.Cmdx("aaa.hash", "select", chain, h)
|
||||||
|
|
||||||
|
if view := m.Confm("temp", hh); view != nil && false { // 加载缓存
|
||||||
|
msg = msg.Spawn()
|
||||||
|
switch data := view["data"].(type) {
|
||||||
|
case map[string][]string:
|
||||||
|
msg.Meta = data
|
||||||
|
case map[string]interface{}:
|
||||||
|
for k, v := range data {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case []interface{}:
|
||||||
|
msg.Add("append", k, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m.Confv("temp", []string{h, "data"}, msg.Meta)
|
||||||
|
}
|
||||||
|
} else { // 添加缓存
|
||||||
|
msg = msg.Spawn().Copy(msg, "append").Cmd("select", m.Optionv("select"))
|
||||||
|
|
||||||
|
m.Confv("temp", hh, map[string]interface{}{
|
||||||
|
"create_time": m.Time(), "expire_time": m.Time("60s"),
|
||||||
|
"type": "select", "meta": chain, "data": msg.Meta,
|
||||||
|
"ship": map[string]interface{}{
|
||||||
|
h: map[string]interface{}{"create_time": m.Time(), "ship": "0", "type": temp["type"], "meta": temp["meta"]},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
m.Confv("temp", []string{h, "ship", hh}, map[string]interface{}{
|
||||||
|
"create_time": m.Time(), "ship": "1", "type": "select", "meta": chain,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.CopyTo(m)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
arg, h := kit.Slice(arg)
|
||||||
|
if h != "" {
|
||||||
|
if temp := m.Confm("temp", h); temp != nil {
|
||||||
|
m.Echo(h)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 缓存列表
|
||||||
|
m.Confm("temp", func(key string, temp map[string]interface{}) {
|
||||||
|
if len(arg) == 0 || strings.HasPrefix(key, arg[0]) || strings.HasSuffix(key, arg[0]) || (temp["type"].(string) == arg[0] && (len(arg) == 1 || strings.Contains(temp["meta"].(string), arg[1]))) {
|
||||||
|
m.Add("append", "key", key)
|
||||||
|
m.Add("append", "create_time", temp["create_time"])
|
||||||
|
m.Add("append", "type", temp["type"])
|
||||||
|
m.Add("append", "meta", temp["meta"])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
m.Sort("create_time", "time_r").Table().Cmd("select", m.Optionv("select"))
|
||||||
|
return
|
||||||
|
}},
|
||||||
"note": &ctx.Command{Name: "note [model [name [type name]...]]|[index [name data...]]|[value name data...]|[name model data...]",
|
"note": &ctx.Command{Name: "note [model [name [type name]...]]|[index [name data...]]|[value name data...]|[name model data...]",
|
||||||
Form: map[string]int{"eq": 2, "begin": 2, "offset": 1, "limit": 1}, Help: "记事", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
Form: map[string]int{"eq": 2, "begin": 2, "offset": 1, "limit": 1}, Help: "记事", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
|
||||||
offset := kit.Int(kit.Select(m.Conf("table", "offset"), m.Option("table.offset")))
|
offset := kit.Int(kit.Select(m.Conf("table", "offset"), m.Option("table.offset")))
|
||||||
@ -873,7 +796,5 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
mdb := &MDB{}
|
ctx.Index.Register(Index, &MDB{Context: Index})
|
||||||
mdb.Context = Index
|
|
||||||
ctx.Index.Register(Index, mdb)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user