xugu_stmt.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package xugu
  2. import (
  3. "database/sql/driver"
  4. "encoding/base64"
  5. "encoding/binary"
  6. "errors"
  7. "fmt"
  8. "unsafe"
  9. )
  10. type xuguStmt struct {
  11. // 上下文连接句柄指针
  12. stmt_conn *xuguConn
  13. // 布尔值,用于标识执行的 SQL 语句是否已准备好
  14. prepared bool
  15. // 接受准备好的 SQL 语句的代码
  16. prename []byte
  17. // 布尔值,用于标识游标是否启用
  18. curopend bool
  19. // 游标名称
  20. curname []byte
  21. // 执行的 SQL 语句中的参数数量
  22. param_count int
  23. mysql string
  24. // 上下文结果集句柄指针
  25. result unsafe.Pointer
  26. }
  27. /* Collect error information from the database server */
  28. func (self *xuguStmt) get_error() error {
  29. // message := cgo_c_calloc(ERROR_BUFF_SIZE)
  30. // defer func() {
  31. // cgo_c_free(unsafe.Pointer(message))
  32. // }()
  33. // var length C.int
  34. // cgo_xgc_error(&self.stmt_conn, message, &length)
  35. // return errors.New(C.GoString(message))
  36. return errors.New("xuguStmt error")
  37. }
  38. /* {{ */
  39. func (stmt *xuguStmt) Close() error {
  40. fmt.Println("\n>>>>>> (stmt *xuguStmt) Close method called")
  41. return nil
  42. }
  43. func (stmt *xuguStmt) NumInput() int {
  44. fmt.Println("\n>>>>>NumInput method called")
  45. parser := &xuguParse{
  46. bind_type: 0,
  47. param_count: 0,
  48. position: 0,
  49. }
  50. fmt.Println("stmt.mysql: ", stmt.mysql)
  51. return parser.assertParamCount(stmt.mysql)
  52. //return 0
  53. }
  54. func (stmt *xuguStmt) Exec(args []driver.Value) (driver.Result, error) {
  55. fmt.Println(">>>>>>>Exec method called")
  56. result := &xuguResult{
  57. affectedRows: 0,
  58. insertId: 0,
  59. }
  60. //最终发送消息给服务器
  61. XGC_Execute(stmt.stmt_conn, stmt.mysql)
  62. //处理服务器返回
  63. stmt.stmt_conn.conn.Read(stmt.stmt_conn.readBuff.buf)
  64. readBuf := stmt.stmt_conn.readBuff
  65. //fmt.Println("Message from server:", (buffer[:n]))
  66. fmt.Println("Message from server:", string(stmt.stmt_conn.readBuff.buf))
  67. //读取一字节
  68. char := readBuf.peekChar()
  69. switch char {
  70. case '$':
  71. fmt.Println("消息类型为$")
  72. case 'I':
  73. readBuf.idx++
  74. fmt.Println("消息类型为I")
  75. //Rowid_Len
  76. Rowid_Len := binary.BigEndian.Uint32(readBuf.readNext(4))
  77. fmt.Println("Rowid_Len", Rowid_Len)
  78. //Rowid_Data // Base64 解码
  79. Rowid_Data, err := base64.StdEncoding.DecodeString(string(readBuf.readNext(int(Rowid_Len))))
  80. if err != nil {
  81. fmt.Println("Error decoding:", err)
  82. }
  83. fmt.Println("Rowid_Data: ", string(Rowid_Data))
  84. case 'U':
  85. fmt.Println("消息类型为U")
  86. case 'D':
  87. fmt.Println("消息类型为D")
  88. case 'E':
  89. fmt.Println("消息类型为E")
  90. case 'W':
  91. fmt.Println("消息类型为W")
  92. case 'M':
  93. fmt.Println("消息类型为M")
  94. }
  95. //return nil, errors.New("Exec not implemented")
  96. return result, nil
  97. }
  98. func (stmt *xuguStmt) Query(args []driver.Value) (driver.Rows, error) {
  99. fmt.Println("\n>>>>>>Query method called")
  100. fmt.Println("stmt.mysql: ", stmt.mysql)
  101. if switchSQLType(stmt.mysql) != SQL_SELECT {
  102. return nil, errors.New("The executed SQL statement is not a SELECT")
  103. }
  104. if !stmt.prepared {
  105. return nil, errors.New("SQL statement is not Prepared")
  106. }
  107. parser := &xuguParse{
  108. bind_type: 0,
  109. param_count: 0,
  110. position: 0,
  111. }
  112. if len(args) != 0 {
  113. for pos, param := range args {
  114. err := parser.assertParamType(param, pos)
  115. if err != nil {
  116. return nil, err
  117. }
  118. }
  119. }
  120. // if len(parser.Val) != parser.assertParamCount(stmt.mysql) {
  121. // return nil, errors.New("The number of parameters does not match")
  122. // }
  123. //最终发送消息给服务器
  124. XGC_Execute(stmt.stmt_conn, stmt.mysql)
  125. //----------------处理服务器查询返回
  126. stmt.stmt_conn.conn.Read(stmt.stmt_conn.readBuff.buf)
  127. //fmt.Println("Message from server:", (buffer[:n]))
  128. fmt.Println("Message from server:", string(stmt.stmt_conn.readBuff.buf))
  129. //fmt.Println("\nMessage from server2:", (stmt.stmt_conn.readBuff.buf))
  130. results, err := parseSelectResult(stmt.stmt_conn.readBuff)
  131. if err != nil {
  132. fmt.Println("parseSelectResult parseSelectResult err", err.Error())
  133. return nil, err
  134. }
  135. fmt.Println("results:", results)
  136. a := &xuguRows{
  137. results: results,
  138. prepared: stmt.prepared,
  139. rows_conn: stmt.stmt_conn,
  140. }
  141. fmt.Println("\n>>>>>>stmt Query end==============================================================")
  142. return a, nil
  143. }