@@ -2,11 +2,13 @@ package proxy
2
2
3
3
import (
4
4
"fmt"
5
+ "github.com/siddontang/go-log/log"
5
6
"github.com/siddontang/mixer/hack"
6
7
. "github.com/siddontang/mixer/mysql"
7
8
"github.com/siddontang/mixer/sqlparser"
8
9
"sort"
9
10
"strings"
11
+ "time"
10
12
)
11
13
12
14
func (c * Conn ) handleShow (sql string , stmt * sqlparser.Show ) error {
@@ -17,6 +19,8 @@ func (c *Conn) handleShow(sql string, stmt *sqlparser.Show) error {
17
19
r , err = c .handleShowDatabases ()
18
20
case "tables" :
19
21
r , err = c .handleShowTables (sql , stmt )
22
+ case "proxy" :
23
+ r , err = c .handleShowProxy (sql , stmt )
20
24
default :
21
25
err = fmt .Errorf ("unsupport show %s now" , sql )
22
26
}
@@ -85,6 +89,95 @@ func (c *Conn) handleShowTables(sql string, stmt *sqlparser.Show) (*Resultset, e
85
89
return c .buildSimpleShowResultset (values , fmt .Sprintf ("Tables_in_%s" , s .db ))
86
90
}
87
91
92
+ func (c * Conn ) handleShowProxy (sql string , stmt * sqlparser.Show ) (* Resultset , error ) {
93
+ var err error
94
+ var r * Resultset
95
+ switch strings .ToLower (stmt .Key ) {
96
+ case "config" :
97
+ r , err = c .handleShowProxyConfig ()
98
+ case "status" :
99
+ r , err = c .handleShowProxyStatus (sql , stmt )
100
+ default :
101
+ err = fmt .Errorf ("Unsupport show proxy [%v] yet, just support [config|status] now." , stmt .Key )
102
+ log .Warn (err .Error ())
103
+ return nil , err
104
+ }
105
+ return r , err
106
+ }
107
+
108
+ func (c * Conn ) handleShowProxyConfig () (* Resultset , error ) {
109
+ var names []string = []string {"Section" , "Key" , "Value" }
110
+ var rows [][]string
111
+ const (
112
+ Section = 0
113
+ Key = 1
114
+ Value = 2
115
+ Column = 3
116
+ )
117
+
118
+ rows = append (rows , []string {"Global_Config" , "Addr" , c .server .cfg .Addr })
119
+ rows = append (rows , []string {"Global_Config" , "User" , c .server .cfg .User })
120
+ rows = append (rows , []string {"Global_Config" , "Password" , c .server .cfg .Password })
121
+ rows = append (rows , []string {"Global_Config" , "LogLevel" , c .server .cfg .LogLevel })
122
+
123
+ rows = append (rows , []string {"Global_Config" , "Schemas_Count" , fmt .Sprintf ("%d" , len (c .server .schemas ))})
124
+ rows = append (rows , []string {"Global_Config" , "Nodes_Count" , fmt .Sprintf ("%d" , len (c .server .nodes ))})
125
+
126
+ // var row []string = make([]string, Column)
127
+
128
+ for db , schema := range c .server .schemas {
129
+ rows = append (rows , []string {"Schemas" , "DB" , db })
130
+
131
+ var nodeNames []string
132
+ var nodeRows [][]string
133
+ for name , node := range schema .nodes {
134
+ nodeNames = append (nodeNames , name )
135
+ var nodeSection = fmt .Sprintf ("Schemas[%s]-Node[ %v ]" , db , name )
136
+
137
+ if node .master != nil {
138
+ nodeRows = append (nodeRows , []string {nodeSection , "Master" , node .master .String ()})
139
+ }
140
+ if node .masterBackup != nil {
141
+ nodeRows = append (nodeRows , []string {nodeSection , "Master_Backup" , node .masterBackup .String ()})
142
+ }
143
+
144
+ if node .slave != nil {
145
+ nodeRows = append (nodeRows , []string {nodeSection , "Slave" , node .slave .String ()})
146
+ }
147
+ nodeRows = append (nodeRows , []string {nodeSection , "Last_Master_Ping" , fmt .Sprintf ("%v" , time .Unix (node .lastMasterPing , 0 ))})
148
+
149
+ nodeRows = append (nodeRows , []string {nodeSection , "Last_Slave_Ping" , fmt .Sprintf ("%v" , time .Unix (node .lastSlavePing , 0 ))})
150
+
151
+ nodeRows = append (nodeRows , []string {nodeSection , "down_after_noalive" , fmt .Sprintf ("%v" , node .downAfterNoAlive )})
152
+
153
+ }
154
+ rows = append (rows , []string {fmt .Sprintf ("Schemas[%s]" , db ), "Nodes_List" , strings .Join (nodeNames , "," )})
155
+ rows = append (rows , []string {fmt .Sprintf ("Schemas[%s]" , db ), "Rule" , schema .rule .String ()})
156
+
157
+ for i := range nodeRows {
158
+ rows = append (rows , nodeRows [i ])
159
+ }
160
+
161
+ }
162
+
163
+ var values [][]interface {} = make ([][]interface {}, len (rows ))
164
+ for i := range rows {
165
+ values [i ] = make ([]interface {}, Column )
166
+ for j := range rows [i ] {
167
+ values [i ][j ] = rows [i ][j ]
168
+ }
169
+ }
170
+
171
+ var r , err = c .buildResultset (names , values )
172
+
173
+ return r , err
174
+ }
175
+
176
+ func (c * Conn ) handleShowProxyStatus (sql string , stmt * sqlparser.Show ) (* Resultset , error ) {
177
+ // TODO: handle like_or_where expr
178
+ return nil , nil
179
+ }
180
+
88
181
func (c * Conn ) buildSimpleShowResultset (values []interface {}, name string ) (* Resultset , error ) {
89
182
90
183
r := new (Resultset )
0 commit comments