xugu_sock.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package xugu
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/binary"
  6. "errors"
  7. "fmt"
  8. )
  9. func xgSockOpenConn(ctx context.Context, pConn *xuguConn) error {
  10. //发送
  11. //fmt.Printf("login database = '%s' user = '%s' password = '%s' version='201' ", pConn.Database, pConn.User, pConn.Password)
  12. // message := "login database = 'SYSTEM' user = 'SYSDBA' password = 'SYSDBA' version='201' "
  13. dsnMessage := generateLoginString(pConn.dsnConfig)
  14. _, err := pConn.conn.Write([]byte(dsnMessage))
  15. if err != nil {
  16. return errors.New("向数据库发起连接失败")
  17. }
  18. buffer := make([]byte, 1)
  19. n, err := pConn.conn.Read(buffer)
  20. if err != nil {
  21. return errors.New("接收数据库连接失败:")
  22. }
  23. if !bytes.Equal(buffer[:n], []byte("K")) {
  24. return errors.New("数据库连接失败")
  25. } else {
  26. return nil
  27. }
  28. }
  29. func sockSendPutStatement(pConn *xuguConn, sql []byte, values *[]xuguValue, paramCount int) error {
  30. if pConn.sendBuff.Len() > 0 {
  31. //将缓冲区重置为空
  32. pConn.sendBuff.Reset()
  33. }
  34. // ?
  35. pConn.sendBuff.Write([]byte("?"))
  36. // Comand_Len
  37. sqlLength := uint32(len(sql))
  38. var networkBytes [4]byte
  39. binary.BigEndian.PutUint32(networkBytes[:], sqlLength)
  40. pConn.sendBuff.Write(networkBytes[:])
  41. // Comand_str
  42. pConn.sendBuff.Write(sql)
  43. //'0' end
  44. binary.BigEndian.PutUint32(networkBytes[:], 0)
  45. pConn.sendBuff.Write([]byte{0})
  46. // Param_num
  47. var Param_num [4]byte
  48. binary.BigEndian.PutUint32(Param_num[:], uint32(paramCount))
  49. pConn.sendBuff.Write(Param_num[:])
  50. if values != nil {
  51. //当缓冲区大于8190字节时,直接发送
  52. // if pConn.sendBuff.Len() > 8190 {
  53. // _, err := pConn.conn.Write(pConn.sendBuff.Bytes())
  54. // if err != nil {
  55. // fmt.Println("sockSend Write failed: ", err)
  56. // return err
  57. // }
  58. // }
  59. //发送后续参数
  60. // Param_num { Param_name_len Param_name Param_INOUT Param_DType Param_Data_Len Param_Data }
  61. for _, value := range *values {
  62. //Param_name_len
  63. if value.paramName == nil {
  64. var Param_name_len [2]byte
  65. pConn.sendBuff.Write(Param_name_len[:])
  66. //Param_name
  67. // var Param_name []byte
  68. // pConn.sendBuff.Write(Param_name)
  69. } else {
  70. var Param_name_len [2]byte
  71. binary.BigEndian.PutUint16(Param_name_len[:], uint16(len(value.paramName)))
  72. pConn.sendBuff.Write(Param_name_len[:])
  73. //Param_name
  74. pConn.sendBuff.Write(value.paramName[:])
  75. }
  76. //Param_INOUT
  77. Param_INOUT := [2]byte{0x1}
  78. pConn.sendBuff.Write(reverseBytes(Param_INOUT[:]))
  79. //Param_DType
  80. var Param_DType [2]byte
  81. binary.BigEndian.PutUint16(Param_DType[:], uint16(value.types))
  82. pConn.sendBuff.Write(Param_DType[:])
  83. //Param_Data_Len 根据DType 修改长度
  84. Param_Data_Len := make([]byte, 4)
  85. binary.BigEndian.PutUint32(Param_Data_Len[:], uint32(value.valueLength))
  86. pConn.sendBuff.Write(Param_Data_Len[:])
  87. //Param_Data 根据DType 修改长度
  88. //Param_Data := make([]byte, value.valueLength)
  89. pConn.sendBuff.Write([]byte(value.value))
  90. }
  91. }
  92. return nil
  93. }
  94. func sockSendExecute(pConn *xuguConn) error {
  95. // fmt.Println("SockSendExecute msg: ", pConn.sendBuff.String())
  96. _, err := pConn.conn.Write(pConn.sendBuff.Bytes())
  97. if err != nil {
  98. return err
  99. }
  100. return nil
  101. }
  102. func xuguSockRecvMsg(pConn *xuguConn) (*allResult, error) {
  103. n, _ := pConn.conn.Read(pConn.readBuff.buf)
  104. pConn.readBuff.length += n
  105. rs, err := parseMsg(&pConn.readBuff, pConn)
  106. if err != nil {
  107. fmt.Println("这里接收到了err: ", err)
  108. return nil, err
  109. }
  110. pConn.readBuff.reset()
  111. return rs, nil
  112. }