xugusql_pconn.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package xugusql
  2. import (
  3. "C"
  4. "context"
  5. "database/sql/driver"
  6. "errors"
  7. "unsafe"
  8. )
  9. type xugusqlConn struct {
  10. conn unsafe.Pointer
  11. /* xugusqlResult */
  12. affectedRows int
  13. insertId int
  14. }
  15. func (self *xugusqlConn) get_error() error {
  16. message := cgo_c_calloc(ERROR_BUFF_SIZE)
  17. defer func() {
  18. cgo_c_free(unsafe.Pointer(message))
  19. }()
  20. var length C.int
  21. cgo_xgc_error(&self.conn, message, &length)
  22. return errors.New(C.GoString(message))
  23. }
  24. func (self *xugusqlConn) Begin() (driver.Tx, error) {
  25. err := self.exec("set auto_commit off;")
  26. if err != nil {
  27. return nil, self.get_error()
  28. }
  29. return &xugusqlTx{tconn: self}, nil
  30. }
  31. func (self *xugusqlConn) Close() error {
  32. re := cgo_xgc_disconnect(&self.conn)
  33. if re < 0 {
  34. return self.get_error()
  35. }
  36. return nil
  37. }
  38. func (self *xugusqlConn) Prepare(query string) (driver.Stmt, error) {
  39. sql := C.CString(query)
  40. defer func() {
  41. cgo_c_free(unsafe.Pointer(sql))
  42. }()
  43. switch cgo_xgc_sql_type(sql) {
  44. case SQL_PROCEDURE:
  45. return nil, errors.New("Prepare does not support stored procedures")
  46. case SQL_UNKNOWN:
  47. return nil, errors.New("Unknown SQL statement type")
  48. case SQL_CREATE:
  49. return nil, errors.New("Prepare does not support DDL.")
  50. }
  51. stmt := &xugusqlStmt{
  52. stmt_conn: self.conn,
  53. prepared: false,
  54. prename: nil,
  55. curopend: false,
  56. curname: nil,
  57. param_count: 0,
  58. result: nil,
  59. mysql: query,
  60. }
  61. if stmt.prename == nil {
  62. stmt.prename = cgo_c_calloc(PREPARE_NAME_BUFF_SIZE)
  63. }
  64. re := cgo_xgc_prepare(&self.conn, sql, stmt.prename)
  65. if re < 0 {
  66. return nil, self.get_error()
  67. }
  68. stmt.prepared = true
  69. return stmt, nil
  70. }
  71. func (self *xugusqlConn) Query(query string,
  72. args []driver.Value) (driver.Rows, error) {
  73. sql := C.CString(query)
  74. defer func() {
  75. cgo_c_free(unsafe.Pointer(sql))
  76. }()
  77. parser := &parse{
  78. bind_type: 0,
  79. param_count: 0,
  80. position: 0,
  81. }
  82. if len(args) != 0 {
  83. for pos, param := range args {
  84. err := parser.assertParamType(param, pos)
  85. if err != nil {
  86. return nil, err
  87. }
  88. }
  89. if len(parser.Val) != parser.assertParamCount(query) {
  90. return nil, errors.New("The number of parameters does not match")
  91. }
  92. switch parser.assertBindType(query) {
  93. case BIND_PARAM_BY_POS:
  94. for pos, param := range parser.Val {
  95. if !param.islob {
  96. re := cgo_xgc_bindparambypos(&self.conn, pos+1, SQL_PARAM_INPUT,
  97. param.types, unsafe.Pointer(param.value), param.buff, &param.length)
  98. if re < 0 {
  99. return nil, self.get_error()
  100. }
  101. } else {
  102. re := cgo_xgc_bindparambypos(&self.conn, pos+1, SQL_PARAM_INPUT,
  103. param.types, unsafe.Pointer(&param.plob), param.buff, &param.length)
  104. if re < 0 {
  105. return nil, self.get_error()
  106. }
  107. }
  108. }
  109. case BIND_PARAM_BY_NAME:
  110. parser.assertParamName(query)
  111. for pos, param := range parser.Val {
  112. if !param.islob {
  113. re := cgo_xgc_bindparambyname(&self.conn, parser.param_names[pos], SQL_PARAM_INPUT,
  114. param.types, unsafe.Pointer(param.value), param.buff, &param.rcode, &param.length)
  115. if re < 0 {
  116. return nil, self.get_error()
  117. }
  118. } else {
  119. re := cgo_xgc_bindparambyname(&self.conn, parser.param_names[pos], SQL_PARAM_INPUT,
  120. param.types, unsafe.Pointer(&param.plob), param.buff, &param.rcode, &param.length)
  121. if re < 0 {
  122. return nil, self.get_error()
  123. }
  124. }
  125. }
  126. }
  127. }
  128. defer func() {
  129. for pos, param := range parser.Val {
  130. if parser.bind_type == BIND_PARAM_BY_NAME {
  131. cgo_c_free(unsafe.Pointer(parser.param_names[pos]))
  132. }
  133. if !param.islob {
  134. cgo_c_free(unsafe.Pointer(param.value))
  135. } else {
  136. cgo_xgc_lob_distroy(&param.plob)
  137. }
  138. }
  139. }()
  140. rows := &xugusqlRows{
  141. rows_conn: self.conn,
  142. result: nil,
  143. lastRowRelt: int(0),
  144. lastRelt: int(0),
  145. prepared: false,
  146. }
  147. var fieldCount, effectCount C.int
  148. var rowCount C.longlong
  149. re := cgo_xgc_exec_with_reader(&self.conn, sql, &rows.result,
  150. &fieldCount, &rowCount, &effectCount)
  151. if re < 0 {
  152. return nil, self.get_error()
  153. }
  154. return rows, nil
  155. }
  156. func (self *xugusqlConn) Exec(query string,
  157. args []driver.Value) (driver.Result, error) {
  158. sql := C.CString(query)
  159. switch cgo_xgc_sql_type(sql) {
  160. case SQL_SELECT:
  161. return nil, errors.New("Exec does not support queries")
  162. case SQL_UNKNOWN:
  163. return nil, errors.New("Unknown SQL statement type")
  164. }
  165. parser := &parse{
  166. bind_type: 0,
  167. param_count: 0,
  168. position: 0,
  169. }
  170. if len(args) != 0 {
  171. for pos, param := range args {
  172. err := parser.assertParamType(param, pos)
  173. if err != nil {
  174. return nil, err
  175. }
  176. }
  177. if len(parser.Val) != parser.assertParamCount(query) {
  178. return nil, errors.New("The number of parameters does not match")
  179. }
  180. switch parser.assertBindType(query) {
  181. case BIND_PARAM_BY_POS:
  182. for pos, param := range parser.Val {
  183. if !param.islob {
  184. re := cgo_xgc_bindparambypos(&self.conn, pos+1, SQL_PARAM_INPUT,
  185. param.types, unsafe.Pointer(param.value), param.buff, &param.length)
  186. if re < 0 {
  187. return nil, self.get_error()
  188. }
  189. } else {
  190. re := cgo_xgc_bindparambypos(&self.conn, pos+1, SQL_PARAM_INPUT,
  191. param.types, unsafe.Pointer(&param.plob), param.buff, &param.length)
  192. if re < 0 {
  193. return nil, self.get_error()
  194. }
  195. }
  196. }
  197. case BIND_PARAM_BY_NAME:
  198. parser.assertParamName(query)
  199. for pos, param := range parser.Val {
  200. if !param.islob {
  201. re := cgo_xgc_bindparambyname(&self.conn, parser.param_names[pos], SQL_PARAM_INPUT,
  202. param.types, unsafe.Pointer(param.value), param.buff, &param.rcode, &param.length)
  203. if re < 0 {
  204. return nil, self.get_error()
  205. }
  206. } else {
  207. re := cgo_xgc_bindparambyname(&self.conn, parser.param_names[pos], SQL_PARAM_INPUT,
  208. param.types, unsafe.Pointer(&param.plob), param.buff, &param.rcode, &param.length)
  209. if re < 0 {
  210. return nil, self.get_error()
  211. }
  212. }
  213. }
  214. }
  215. }
  216. defer func() {
  217. cgo_c_free(unsafe.Pointer(sql))
  218. for pos, param := range parser.Val {
  219. if parser.bind_type == BIND_PARAM_BY_NAME {
  220. cgo_c_free(unsafe.Pointer(parser.param_names[pos]))
  221. }
  222. if !param.islob {
  223. cgo_c_free(unsafe.Pointer(param.value))
  224. } else {
  225. cgo_xgc_lob_distroy(&param.plob)
  226. }
  227. }
  228. }()
  229. self.affectedRows = 0
  230. self.insertId = 0
  231. err := self.exec(query)
  232. if err == nil {
  233. return &xugusqlResult{
  234. affectedRows: int64(self.affectedRows),
  235. insertId: int64(self.insertId),
  236. }, nil
  237. }
  238. return nil, err
  239. }
  240. func (self *xugusqlConn) exec(query string) error {
  241. sql := C.CString(query)
  242. defer func() {
  243. cgo_c_free(unsafe.Pointer(sql))
  244. }()
  245. self.affectedRows = cgo_xgc_execnoquery(&self.conn, sql)
  246. if self.affectedRows < 0 {
  247. return self.get_error()
  248. }
  249. return nil
  250. }
  251. func (self *xugusqlConn) ExecContext(ctx context.Context,
  252. query string, args []driver.NamedValue) (driver.Result, error) {
  253. Value, err := namedValueToValue(args)
  254. if err != nil {
  255. return nil, err
  256. }
  257. return self.Exec(query, Value)
  258. }
  259. func (self *xugusqlConn) Ping(ctx context.Context) error {
  260. sql := C.CString("select count(*) from dual;")
  261. defer func() {
  262. cgo_c_free(unsafe.Pointer(sql))
  263. }()
  264. var fieldCount, effectCount C.int
  265. var rowCount C.longlong
  266. var result unsafe.Pointer
  267. re := cgo_xgc_exec_with_reader(&self.conn, sql, &result,
  268. &fieldCount, &rowCount, &effectCount)
  269. if re < 0 {
  270. return self.get_error()
  271. }
  272. return nil
  273. }