xugu_sock.go 3.4 KB

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