sqlconn.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package sqlx
  2. import (
  3. "database/sql"
  4. "github.com/tal-tech/go-zero/core/breaker"
  5. )
  6. // ErrNotFound is an alias of sql.ErrNoRows
  7. var ErrNotFound = sql.ErrNoRows
  8. type (
  9. // Session stands for raw connections or transaction sessions
  10. Session interface {
  11. Exec(query string, args ...interface{}) (sql.Result, error)
  12. Prepare(query string) (StmtSession, error)
  13. QueryRow(v interface{}, query string, args ...interface{}) error
  14. QueryRowPartial(v interface{}, query string, args ...interface{}) error
  15. QueryRows(v interface{}, query string, args ...interface{}) error
  16. QueryRowsPartial(v interface{}, query string, args ...interface{}) error
  17. }
  18. // SqlConn only stands for raw connections, so Transact method can be called.
  19. SqlConn interface {
  20. Session
  21. Transact(func(session Session) error) error
  22. }
  23. // SqlOption defines the method to customize a sql connection.
  24. SqlOption func(*commonSqlConn)
  25. // StmtSession interface represents a session that can be used to execute statements.
  26. StmtSession interface {
  27. Close() error
  28. Exec(args ...interface{}) (sql.Result, error)
  29. QueryRow(v interface{}, args ...interface{}) error
  30. QueryRowPartial(v interface{}, args ...interface{}) error
  31. QueryRows(v interface{}, args ...interface{}) error
  32. QueryRowsPartial(v interface{}, args ...interface{}) error
  33. }
  34. // thread-safe
  35. // Because CORBA doesn't support PREPARE, so we need to combine the
  36. // query arguments into one string and do underlying query without arguments
  37. commonSqlConn struct {
  38. driverName string
  39. datasource string
  40. beginTx beginnable
  41. brk breaker.Breaker
  42. accept func(error) bool
  43. }
  44. sessionConn interface {
  45. Exec(query string, args ...interface{}) (sql.Result, error)
  46. Query(query string, args ...interface{}) (*sql.Rows, error)
  47. }
  48. statement struct {
  49. stmt *sql.Stmt
  50. }
  51. stmtConn interface {
  52. Exec(args ...interface{}) (sql.Result, error)
  53. Query(args ...interface{}) (*sql.Rows, error)
  54. }
  55. )
  56. // NewSqlConn returns a SqlConn with given driver name and datasource.
  57. func NewSqlConn(driverName, datasource string, opts ...SqlOption) SqlConn {
  58. conn := &commonSqlConn{
  59. driverName: driverName,
  60. datasource: datasource,
  61. beginTx: begin,
  62. brk: breaker.NewBreaker(),
  63. }
  64. for _, opt := range opts {
  65. opt(conn)
  66. }
  67. return conn
  68. }
  69. func (db *commonSqlConn) Exec(q string, args ...interface{}) (result sql.Result, err error) {
  70. err = db.brk.DoWithAcceptable(func() error {
  71. var conn *sql.DB
  72. conn, err = getSqlConn(db.driverName, db.datasource)
  73. if err != nil {
  74. logInstanceError(db.datasource, err)
  75. return err
  76. }
  77. result, err = exec(conn, q, args...)
  78. return err
  79. }, db.acceptable)
  80. return
  81. }
  82. func (db *commonSqlConn) Prepare(query string) (stmt StmtSession, err error) {
  83. err = db.brk.DoWithAcceptable(func() error {
  84. var conn *sql.DB
  85. conn, err = getSqlConn(db.driverName, db.datasource)
  86. if err != nil {
  87. logInstanceError(db.datasource, err)
  88. return err
  89. }
  90. st, err := conn.Prepare(query)
  91. if err != nil {
  92. return err
  93. }
  94. stmt = statement{
  95. stmt: st,
  96. }
  97. return nil
  98. }, db.acceptable)
  99. return
  100. }
  101. func (db *commonSqlConn) QueryRow(v interface{}, q string, args ...interface{}) error {
  102. return db.queryRows(func(rows *sql.Rows) error {
  103. return unmarshalRow(v, rows, true)
  104. }, q, args...)
  105. }
  106. func (db *commonSqlConn) QueryRowPartial(v interface{}, q string, args ...interface{}) error {
  107. return db.queryRows(func(rows *sql.Rows) error {
  108. return unmarshalRow(v, rows, false)
  109. }, q, args...)
  110. }
  111. func (db *commonSqlConn) QueryRows(v interface{}, q string, args ...interface{}) error {
  112. return db.queryRows(func(rows *sql.Rows) error {
  113. return unmarshalRows(v, rows, true)
  114. }, q, args...)
  115. }
  116. func (db *commonSqlConn) QueryRowsPartial(v interface{}, q string, args ...interface{}) error {
  117. return db.queryRows(func(rows *sql.Rows) error {
  118. return unmarshalRows(v, rows, false)
  119. }, q, args...)
  120. }
  121. func (db *commonSqlConn) Transact(fn func(Session) error) error {
  122. return db.brk.DoWithAcceptable(func() error {
  123. return transact(db, db.beginTx, fn)
  124. }, db.acceptable)
  125. }
  126. func (db *commonSqlConn) acceptable(err error) bool {
  127. ok := err == nil || err == sql.ErrNoRows || err == sql.ErrTxDone
  128. if db.accept == nil {
  129. return ok
  130. }
  131. return ok || db.accept(err)
  132. }
  133. func (db *commonSqlConn) queryRows(scanner func(*sql.Rows) error, q string, args ...interface{}) error {
  134. var qerr error
  135. return db.brk.DoWithAcceptable(func() error {
  136. conn, err := getSqlConn(db.driverName, db.datasource)
  137. if err != nil {
  138. logInstanceError(db.datasource, err)
  139. return err
  140. }
  141. return query(conn, func(rows *sql.Rows) error {
  142. qerr = scanner(rows)
  143. return qerr
  144. }, q, args...)
  145. }, func(err error) bool {
  146. return qerr == err || db.acceptable(err)
  147. })
  148. }
  149. func (s statement) Close() error {
  150. return s.stmt.Close()
  151. }
  152. func (s statement) Exec(args ...interface{}) (sql.Result, error) {
  153. return execStmt(s.stmt, args...)
  154. }
  155. func (s statement) QueryRow(v interface{}, args ...interface{}) error {
  156. return queryStmt(s.stmt, func(rows *sql.Rows) error {
  157. return unmarshalRow(v, rows, true)
  158. }, args...)
  159. }
  160. func (s statement) QueryRowPartial(v interface{}, args ...interface{}) error {
  161. return queryStmt(s.stmt, func(rows *sql.Rows) error {
  162. return unmarshalRow(v, rows, false)
  163. }, args...)
  164. }
  165. func (s statement) QueryRows(v interface{}, args ...interface{}) error {
  166. return queryStmt(s.stmt, func(rows *sql.Rows) error {
  167. return unmarshalRows(v, rows, true)
  168. }, args...)
  169. }
  170. func (s statement) QueryRowsPartial(v interface{}, args ...interface{}) error {
  171. return queryStmt(s.stmt, func(rows *sql.Rows) error {
  172. return unmarshalRows(v, rows, false)
  173. }, args...)
  174. }