xugu_stmt.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. func (stmt *xuguStmt) Close() error {
  25. fmt.Println("\n>>>>>> (stmt *xuguStmt) Close method called")
  26. return nil
  27. }
  28. func (stmt *xuguStmt) NumInput() int {
  29. fmt.Println("\n>>>>>(stmt *xuguStmt) NumInput()")
  30. // parser := &xuguParse{
  31. // bind_type: 0,
  32. // param_count: 0,
  33. // }
  34. fmt.Println("stmt.mysql: ", stmt.mysql)
  35. return assertParamCount(stmt.mysql)
  36. //return 0
  37. }
  38. func (stmt *xuguStmt) Exec(args []driver.Value) (driver.Result, error) {
  39. gt("stmt Exec")
  40. defer gt("stmt Exec end")
  41. stmt.stmt_conn.mu.Lock()
  42. defer stmt.stmt_conn.mu.Unlock()
  43. //send msg
  44. //如果有参数
  45. if stmt.paramCount > 0 && len(args) > 0 {
  46. values := []xuguValue{}
  47. for _, param := range args {
  48. assertParamType(param, &values)
  49. //fmt.Printf("Field Name: %s, Field Type: %s, Field Value: %v\n", v1, v1, v1)
  50. }
  51. sockSendPutStatement(stmt.stmt_conn, stmt.prename, &values, stmt.paramCount)
  52. sockSendExecute(stmt.stmt_conn)
  53. //没有参数
  54. } else {
  55. sockSendPutStatement(stmt.stmt_conn, []byte(stmt.mysql), nil, 0)
  56. sockSendExecute(stmt.stmt_conn)
  57. }
  58. //recv msg
  59. aR, err := xuguSockRecvMsg(stmt.stmt_conn)
  60. if err != nil {
  61. return nil, err
  62. }
  63. switch aR.rt {
  64. case selectResult:
  65. return nil, errors.New("select result type is error")
  66. case errInfo:
  67. return nil, errors.New(string(aR.e.ErrStr))
  68. case warnInfo:
  69. return nil, errors.New(string(aR.w.WarnStr))
  70. default:
  71. return &xuguResult{
  72. affectedRows: int64(0),
  73. insertId: int64(0),
  74. }, nil
  75. }
  76. }
  77. func (stmt *xuguStmt) Query(args []driver.Value) (driver.Rows, error) {
  78. return nil, nil
  79. }