forked from dataux/dataux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_functions.go
66 lines (58 loc) · 1.42 KB
/
mysql_functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package mysqlfe
import (
"sync"
u "github.com/araddon/gou"
"github.com/araddon/qlbridge/expr"
"github.com/araddon/qlbridge/value"
)
var _ = u.EMPTY
var loadOnce sync.Once
func init() {
LoadMySqlFunctions()
}
func LoadMySqlFunctions() {
loadOnce.Do(func() {
expr.FuncAdd("current_user", CurrentUser)
expr.FuncAdd("connection_id", ConnectionId)
expr.FuncAdd("database", DatabaseName)
})
}
// DatabaseName: name of the database
//
// DATABASE() => "your_db", true
//
func DatabaseName(ctx expr.EvalContext) (value.StringValue, bool) {
if dbVal, ok := ctx.Get("@@database"); ok {
if dbValStr, ok := dbVal.(value.StringValue); ok {
return dbValStr, true
}
}
return value.NewStringValue(""), true
}
// ConnectionId: id of current connection
//
// connection_id() => 11, true
//
func ConnectionId(ctx expr.EvalContext) (value.IntValue, bool) {
if connVal, ok := ctx.Get("@@connection_id"); ok {
if connInt, ok := connVal.(value.IntValue); ok {
return connInt, true
}
}
//u.Infof("ConnectionId: %#v", ctx)
return value.NewIntValue(1), true
}
// CurrentUser: username of current user
//
// current_user() => user, true
//
func CurrentUser(ctx expr.EvalContext) (value.StringValue, bool) {
if ctx == nil {
return value.NewStringValue("root"), true
}
v, ok := ctx.Get("@@user")
if !ok {
return value.EmptyStringValue, false
}
return value.NewStringValue(v.ToString()), true
}