xugu_stmt.go 5.2 KB

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