req.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package client
  2. import (
  3. "github.com/go-mysql-org/go-mysql/utils"
  4. )
  5. func (c *Conn) writeCommand(command byte) error {
  6. c.ResetSequence()
  7. return c.WritePacket([]byte{
  8. 0x01, //1 bytes long
  9. 0x00,
  10. 0x00,
  11. 0x00, //sequence
  12. command,
  13. })
  14. }
  15. func (c *Conn) writeCommandBuf(command byte, arg []byte) error {
  16. c.ResetSequence()
  17. length := len(arg) + 1
  18. data := utils.ByteSliceGet(length + 4)
  19. data.B[4] = command
  20. copy(data.B[5:], arg)
  21. err := c.WritePacket(data.B)
  22. utils.ByteSlicePut(data)
  23. return err
  24. }
  25. func (c *Conn) writeCommandStr(command byte, arg string) error {
  26. return c.writeCommandBuf(command, utils.StringToByteSlice(arg))
  27. }
  28. func (c *Conn) writeCommandUint32(command byte, arg uint32) error {
  29. c.ResetSequence()
  30. return c.WritePacket([]byte{
  31. 0x05, //5 bytes long
  32. 0x00,
  33. 0x00,
  34. 0x00, //sequence
  35. command,
  36. byte(arg),
  37. byte(arg >> 8),
  38. byte(arg >> 16),
  39. byte(arg >> 24),
  40. })
  41. }
  42. func (c *Conn) writeCommandStrStr(command byte, arg1 string, arg2 string) error {
  43. c.ResetSequence()
  44. data := make([]byte, 4, 6+len(arg1)+len(arg2))
  45. data = append(data, command)
  46. data = append(data, arg1...)
  47. data = append(data, 0)
  48. data = append(data, arg2...)
  49. return c.WritePacket(data)
  50. }