xugusql_pconn.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package xugu
  2. import (
  3. "bytes"
  4. "context"
  5. "database/sql/driver"
  6. "errors"
  7. "fmt"
  8. "net"
  9. "sync"
  10. )
  11. type xugusqlConn struct {
  12. conn net.Conn
  13. /* xugusqlResult */
  14. affectedRows int
  15. insertId int
  16. havePrepare int //default 0
  17. prepareNo int //fashengqi
  18. params *XGCSParam
  19. prepareName string
  20. presPrepareCata *Result
  21. mu sync.Mutex
  22. useSSL bool
  23. turingDataR interface{} // 加密相关的数据,具体类型根据实际情况定义
  24. errStr []byte
  25. dsnConfig
  26. Conn_Attrs
  27. }
  28. type Conn_Attrs struct {
  29. bkChar byte
  30. sendBuff bytes.Buffer
  31. }
  32. type dsnConfig struct {
  33. IP string
  34. Port string
  35. Database string
  36. User string
  37. Password string
  38. Encryptor string //加密库的解密口令
  39. CharSet string //客户端使用的字符集名
  40. TimeZone string
  41. IsoLevel string //事务隔离级别
  42. LockTimeout string //加锁超时
  43. AutoCommit string
  44. StrictCommit string
  45. Result string
  46. ReturnSchema string
  47. ReturnCursorID string
  48. LobRet string
  49. ReturnRowid string
  50. Version string
  51. }
  52. func (xgConn *xugusqlConn) get_error() error {
  53. return nil
  54. }
  55. func (xgConn *xugusqlConn) Begin() (driver.Tx, error) {
  56. err := xgConn.exec("set auto_commit off;")
  57. if err != nil {
  58. return nil, xgConn.get_error()
  59. }
  60. return &xugusqlTx{tconn: xgConn}, nil
  61. }
  62. func (xgConn *xugusqlConn) exec(query string) error {
  63. return nil
  64. }
  65. func (xgConn *xugusqlConn) Prepare(query string) (driver.Stmt, error) {
  66. switch xgSqlType(query) {
  67. case SQL_PROCEDURE:
  68. return nil, errors.New("Prepare does not support stored procedures")
  69. case SQL_UNKNOWN:
  70. return nil, errors.New("Unknown SQL statement type")
  71. case SQL_CREATE:
  72. return nil, errors.New("Prepare does not support DDL.")
  73. }
  74. stmt := &xugusqlStmt{
  75. stmt_conn: xgConn.conn,
  76. prepared: false,
  77. prename: "",
  78. curopend: false,
  79. curname: "",
  80. param_count: 0,
  81. result: nil,
  82. mysql: query,
  83. }
  84. var sql string
  85. re, err := xgSockPrepare2(xgConn, sql, &xgConn.prepareName)
  86. if re < 0 || err != nil {
  87. return nil, xgConn.get_error()
  88. }
  89. stmt.prepared = true
  90. return stmt, nil
  91. }
  92. func (xgConn *xugusqlConn) Close() error {
  93. fmt.Println("Close connection")
  94. err := xgConn.conn.Close()
  95. if err != nil {
  96. fmt.Println("Close connection error")
  97. return err
  98. }
  99. return nil
  100. }
  101. func (xgConn *xugusqlConn) Ping(ctx context.Context) error {
  102. // sql := C.CString("select count(*) from dual;")
  103. // defer func() {
  104. // cgo_c_free(unsafe.Pointer(sql))
  105. // }()
  106. // var fieldCount, effectCount C.int
  107. // var rowCount C.longlong
  108. // var result unsafe.Pointer
  109. // re := cgo_xgc_exec_with_reader(&self.conn, sql, &result,
  110. // &fieldCount, &rowCount, &effectCount)
  111. // if re < 0 {
  112. // return self.get_error()
  113. // }
  114. return nil
  115. }