@@ -11,6 +11,7 @@ import (
1111 "github.com/siddontang/go/hack"
1212)
1313
14+ // Handler is what a server needs to implement the client-server protocol
1415type Handler interface {
1516 //handle COM_INIT_DB command, you can check whether the dbName is valid, or other.
1617 UseDB (dbName string ) error
@@ -33,13 +34,16 @@ type Handler interface {
3334 HandleOtherCommand (cmd byte , data []byte ) error
3435}
3536
37+ // ReplicationHandler is for handlers that want to implement the replication protocol
3638type ReplicationHandler interface {
3739 // handle Replication command
3840 HandleRegisterSlave (data []byte ) error
3941 HandleBinlogDump (pos Position ) (* replication.BinlogStreamer , error )
4042 HandleBinlogDumpGTID (gtidSet * MysqlGTIDSet ) (* replication.BinlogStreamer , error )
4143}
4244
45+ // HandleCommand is handling commands received by the server
46+ // https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_command_phase.html
4347func (c * Conn ) HandleCommand () error {
4448 if c .Conn == nil {
4549 return fmt .Errorf ("connection closed" )
@@ -180,17 +184,22 @@ func (c *Conn) dispatch(data []byte) interface{} {
180184 }
181185}
182186
187+ // EmptyHandler is a mostly empty implementation for demonstration purposes
183188type EmptyHandler struct {
184189}
185190
191+ // EmptyReplicationHandler is a empty handler that implements the replication protocol
186192type EmptyReplicationHandler struct {
187193 EmptyHandler
188194}
189195
196+ // UseDB is called for COM_INIT_DB
190197func (h EmptyHandler ) UseDB (dbName string ) error {
191198 log .Printf ("Received: UseDB %s" , dbName )
192199 return nil
193200}
201+
202+ // HandleQuery is called for COM_QUERY
194203func (h EmptyHandler ) HandleQuery (query string ) (* Result , error ) {
195204 log .Printf ("Received: Query: %s" , query )
196205
@@ -217,41 +226,59 @@ func (h EmptyHandler) HandleQuery(query string) (*Result, error) {
217226 return nil , fmt .Errorf ("not supported now" )
218227}
219228
229+ // HandleFieldList is called for COM_FIELD_LIST packets
230+ // Note that COM_FIELD_LIST has been deprecated since MySQL 5.7.11
231+ // https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_field_list.html
220232func (h EmptyHandler ) HandleFieldList (table string , fieldWildcard string ) ([]* Field , error ) {
221- log .Println ("Received: FieldList" )
233+ log .Printf ("Received: FieldList: table=%s, fieldWildcard:%s" , table , fieldWildcard )
222234 return nil , fmt .Errorf ("not supported now" )
223235}
236+
237+ // HandleStmtPrepare is called for COM_STMT_PREPARE
224238func (h EmptyHandler ) HandleStmtPrepare (query string ) (int , int , interface {}, error ) {
225239 log .Printf ("Received: StmtPrepare: %s" , query )
226240 return 0 , 0 , nil , fmt .Errorf ("not supported now" )
227241}
242+
243+ // 'context' isn't used but replacing it with `_` would remove important information for who
244+ // wants to extend this later.
245+ //revive:disable:unused-parameter
246+
247+ // HandleStmtExecute is called for COM_STMT_EXECUTE
228248func (h EmptyHandler ) HandleStmtExecute (context interface {}, query string , args []interface {}) (* Result , error ) {
229- log .Println ("Received: StmtExecute" )
249+ log .Printf ("Received: StmtExecute: %s (args: %v)" , query , args )
230250 return nil , fmt .Errorf ("not supported now" )
231251}
232252
253+ // HandleStmtClose is called for COM_STMT_CLOSE
233254func (h EmptyHandler ) HandleStmtClose (context interface {}) error {
234255 log .Println ("Received: StmtClose" )
235256 return nil
236257}
237258
259+ //revive:enable:unused-parameter
260+
261+ // HandleRegisterSlave is called for COM_REGISTER_SLAVE
238262func (h EmptyReplicationHandler ) HandleRegisterSlave (data []byte ) error {
239- log .Println ("Received: RegisterSlave" )
263+ log .Printf ("Received: RegisterSlave: %x" , data )
240264 return fmt .Errorf ("not supported now" )
241265}
242266
267+ // HandleBinlogDump is called for COM_BINLOG_DUMP (non-GTID)
243268func (h EmptyReplicationHandler ) HandleBinlogDump (pos Position ) (* replication.BinlogStreamer , error ) {
244- log .Println ("Received: BinlogDump" )
269+ log .Printf ("Received: BinlogDump: pos=%s" , pos . String () )
245270 return nil , fmt .Errorf ("not supported now" )
246271}
247272
273+ // HandleBinlogDumpGTID is called for COM_BINLOG_DUMP_GTID
248274func (h EmptyReplicationHandler ) HandleBinlogDumpGTID (gtidSet * MysqlGTIDSet ) (* replication.BinlogStreamer , error ) {
249- log .Println ("Received: BinlogDumpGTID" )
275+ log .Printf ("Received: BinlogDumpGTID: gtidSet=%s" , gtidSet . String () )
250276 return nil , fmt .Errorf ("not supported now" )
251277}
252278
279+ // HandleOtherCommand is called for commands not handled elsewhere
253280func (h EmptyHandler ) HandleOtherCommand (cmd byte , data []byte ) error {
254- log .Println ("Received: OtherCommand" )
281+ log .Printf ("Received: OtherCommand: cmd=%x, data=%x" , cmd , data )
255282 return NewError (
256283 ER_UNKNOWN_ERROR ,
257284 fmt .Sprintf ("command %d is not supported now" , cmd ),
0 commit comments