command.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package native
  2. import (
  3. "log"
  4. )
  5. //import "log"
  6. // _COM_QUIT, _COM_STATISTICS, _COM_PROCESS_INFO, _COM_DEBUG, _COM_PING:
  7. func (my *Conn) sendCmd(cmd byte) {
  8. my.seq = 0
  9. pw := my.newPktWriter(1)
  10. pw.writeByte(cmd)
  11. if my.Debug {
  12. log.Printf("[%2d <-] Command packet: Cmd=0x%x", my.seq-1, cmd)
  13. }
  14. }
  15. // _COM_QUERY, _COM_INIT_DB, _COM_CREATE_DB, _COM_DROP_DB, _COM_STMT_PREPARE:
  16. func (my *Conn) sendCmdStr(cmd byte, s string) {
  17. my.seq = 0
  18. pw := my.newPktWriter(1 + len(s))
  19. pw.writeByte(cmd)
  20. pw.write([]byte(s))
  21. if my.Debug {
  22. log.Printf("[%2d <-] Command packet: Cmd=0x%x %s", my.seq-1, cmd, s)
  23. }
  24. }
  25. // _COM_PROCESS_KILL, _COM_STMT_CLOSE, _COM_STMT_RESET:
  26. func (my *Conn) sendCmdU32(cmd byte, u uint32) {
  27. my.seq = 0
  28. pw := my.newPktWriter(1 + 4)
  29. pw.writeByte(cmd)
  30. pw.writeU32(u)
  31. if my.Debug {
  32. log.Printf("[%2d <-] Command packet: Cmd=0x%x %d", my.seq-1, cmd, u)
  33. }
  34. }
  35. func (my *Conn) sendLongData(stmtid uint32, pnum uint16, data []byte) {
  36. my.seq = 0
  37. pw := my.newPktWriter(1 + 4 + 2 + len(data))
  38. pw.writeByte(_COM_STMT_SEND_LONG_DATA)
  39. pw.writeU32(stmtid) // Statement ID
  40. pw.writeU16(pnum) // Parameter number
  41. pw.write(data) // payload
  42. if my.Debug {
  43. log.Printf("[%2d <-] SendLongData packet: pnum=%d", my.seq-1, pnum)
  44. }
  45. }
  46. /*func (my *Conn) sendCmd(cmd byte, argv ...interface{}) {
  47. // Reset sequence number
  48. my.seq = 0
  49. // Write command
  50. switch cmd {
  51. case _COM_QUERY, _COM_INIT_DB, _COM_CREATE_DB, _COM_DROP_DB,
  52. _COM_STMT_PREPARE:
  53. pw := my.newPktWriter(1 + lenBS(argv[0]))
  54. writeByte(pw, cmd)
  55. writeBS(pw, argv[0])
  56. case _COM_STMT_SEND_LONG_DATA:
  57. pw := my.newPktWriter(1 + 4 + 2 + lenBS(argv[2]))
  58. writeByte(pw, cmd)
  59. writeU32(pw, argv[0].(uint32)) // Statement ID
  60. writeU16(pw, argv[1].(uint16)) // Parameter number
  61. writeBS(pw, argv[2]) // payload
  62. case _COM_QUIT, _COM_STATISTICS, _COM_PROCESS_INFO, _COM_DEBUG, _COM_PING:
  63. pw := my.newPktWriter(1)
  64. writeByte(pw, cmd)
  65. case _COM_FIELD_LIST:
  66. pay_len := 1 + lenBS(argv[0]) + 1
  67. if len(argv) > 1 {
  68. pay_len += lenBS(argv[1])
  69. }
  70. pw := my.newPktWriter(pay_len)
  71. writeByte(pw, cmd)
  72. writeNT(pw, argv[0])
  73. if len(argv) > 1 {
  74. writeBS(pw, argv[1])
  75. }
  76. case _COM_TABLE_DUMP:
  77. pw := my.newPktWriter(1 + lenLC(argv[0]) + lenLC(argv[1]))
  78. writeByte(pw, cmd)
  79. writeLC(pw, argv[0])
  80. writeLC(pw, argv[1])
  81. case _COM_REFRESH, _COM_SHUTDOWN:
  82. pw := my.newPktWriter(1 + 1)
  83. writeByte(pw, cmd)
  84. writeByte(pw, argv[0].(byte))
  85. case _COM_STMT_FETCH:
  86. pw := my.newPktWriter(1 + 4 + 4)
  87. writeByte(pw, cmd)
  88. writeU32(pw, argv[0].(uint32))
  89. writeU32(pw, argv[1].(uint32))
  90. case _COM_PROCESS_KILL, _COM_STMT_CLOSE, _COM_STMT_RESET:
  91. pw := my.newPktWriter(1 + 4)
  92. writeByte(pw, cmd)
  93. writeU32(pw, argv[0].(uint32))
  94. case _COM_SET_OPTION:
  95. pw := my.newPktWriter(1 + 2)
  96. writeByte(pw, cmd)
  97. writeU16(pw, argv[0].(uint16))
  98. case _COM_CHANGE_USER:
  99. pw := my.newPktWriter(
  100. 1 + lenBS(argv[0]) + 1 + lenLC(argv[1]) + lenBS(argv[2]) + 1,
  101. )
  102. writeByte(pw, cmd)
  103. writeNT(pw, argv[0]) // User name
  104. writeLC(pw, argv[1]) // Scrambled password
  105. writeNT(pw, argv[2]) // Database name
  106. //writeU16(pw, argv[3]) // Character set number (since 5.1.23?)
  107. case _COM_BINLOG_DUMP:
  108. pay_len := 1 + 4 + 2 + 4
  109. if len(argv) > 3 {
  110. pay_len += lenBS(argv[3])
  111. }
  112. pw := my.newPktWriter(pay_len)
  113. writeByte(pw, cmd)
  114. writeU32(pw, argv[0].(uint32)) // Start position
  115. writeU16(pw, argv[1].(uint16)) // Flags
  116. writeU32(pw, argv[2].(uint32)) // Slave server id
  117. if len(argv) > 3 {
  118. writeBS(pw, argv[3])
  119. }
  120. // TODO: case COM_REGISTER_SLAVE:
  121. default:
  122. panic("Unknown code for MySQL command")
  123. }
  124. if my.Debug {
  125. log.Printf("[%2d <-] Command packet: Cmd=0x%x", my.seq-1, cmd)
  126. }
  127. }*/