xugusql_connector.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package xugusql
  2. import (
  3. "C"
  4. "context"
  5. "database/sql/driver"
  6. "strings"
  7. "unsafe"
  8. )
  9. const (
  10. ERROR_BUFF_SIZE uint = 1024
  11. PREPARE_NAME_BUFF_SIZE uint = 128
  12. CURSOR_NAME_BUFF_SIZE uint = 128
  13. ROWID_BUFF_SIZE uint = 256
  14. COLUMN_NAME_BUFF_SIZE uint = 256
  15. FIELD_BUFF_SIZE uint = 4096
  16. LOB_BUFF_SIZE uint = 8
  17. RET_NO_DATA int = 100
  18. SQL_UNKNOWN int = 0
  19. SQL_SELECT int = 4
  20. SQL_CREATE int = 5
  21. SQL_PROCEDURE int = 10
  22. SQL_PARAM_INPUT int = 1
  23. SQL_PARAM_OUTPUT int = 2
  24. SQL_PARAM_INPUTOUTPUT int = 3
  25. SQL_PARAM_RETURNVALUE int = 6
  26. SQL_XG_C_CHAR int = 2
  27. SQL_XG_C_CLOB int = 41
  28. SQL_XG_C_BLOB int = 42
  29. SQL_XG_C_NULL int = -11
  30. BIND_PARAM_BY_NAME int = 62
  31. BIND_PARAM_BY_POS int = 63
  32. )
  33. type connector struct {
  34. dsn string
  35. }
  36. // Connect implements driver.Connector interface.
  37. // Connect returns a connection to the database.
  38. func (self *connector) Connect(ctx context.Context) (driver.Conn, error) {
  39. obj := &xugusqlConn{conn: nil}
  40. connKeyValue := C.CString(self.dsn)
  41. defer func() {
  42. cgo_c_free(unsafe.Pointer(connKeyValue))
  43. }()
  44. pos := strings.Index(strings.ToUpper(self.dsn), "IPS=")
  45. if pos != -1 {
  46. IPS_COUNTER++
  47. re := cgo_xgc_connect_ips(connKeyValue, &obj.conn)
  48. if re < 0 {
  49. return nil, obj.get_error()
  50. }
  51. } else {
  52. re := cgo_xgc_connect(connKeyValue, &obj.conn)
  53. if re < 0 {
  54. return nil, obj.get_error()
  55. }
  56. }
  57. return obj, nil
  58. }
  59. // Driver implements driver.Connector interface.
  60. // Driver returns &XuguDriver{}
  61. func (self *connector) Driver() driver.Driver {
  62. return &XuguDriver{}
  63. }