xugu_stmt.go 6.1 KB

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