sqlconn.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package sqlx
  2. import (
  3. "context"
  4. "database/sql"
  5. "github.com/zeromicro/go-zero/core/breaker"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. )
  8. // spanName is used to identify the span name for the SQL execution.
  9. const spanName = "sql"
  10. // ErrNotFound is an alias of sql.ErrNoRows
  11. var ErrNotFound = sql.ErrNoRows
  12. type (
  13. // Session stands for raw connections or transaction sessions
  14. Session interface {
  15. Exec(query string, args ...interface{}) (sql.Result, error)
  16. ExecCtx(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
  17. Prepare(query string) (StmtSession, error)
  18. PrepareCtx(ctx context.Context, query string) (StmtSession, error)
  19. QueryRow(v interface{}, query string, args ...interface{}) error
  20. QueryRowCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error
  21. QueryRowPartial(v interface{}, query string, args ...interface{}) error
  22. QueryRowPartialCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error
  23. QueryRows(v interface{}, query string, args ...interface{}) error
  24. QueryRowsCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error
  25. QueryRowsPartial(v interface{}, query string, args ...interface{}) error
  26. QueryRowsPartialCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error
  27. }
  28. // SqlConn only stands for raw connections, so Transact method can be called.
  29. SqlConn interface {
  30. Session
  31. // RawDB is for other ORM to operate with, use it with caution.
  32. // Notice: don't close it.
  33. RawDB() (*sql.DB, error)
  34. Transact(fn func(Session) error) error
  35. TransactCtx(ctx context.Context, fn func(context.Context, Session) error) error
  36. }
  37. // SqlOption defines the method to customize a sql connection.
  38. SqlOption func(*commonSqlConn)
  39. // StmtSession interface represents a session that can be used to execute statements.
  40. StmtSession interface {
  41. Close() error
  42. Exec(args ...interface{}) (sql.Result, error)
  43. ExecCtx(ctx context.Context, args ...interface{}) (sql.Result, error)
  44. QueryRow(v interface{}, args ...interface{}) error
  45. QueryRowCtx(ctx context.Context, v interface{}, args ...interface{}) error
  46. QueryRowPartial(v interface{}, args ...interface{}) error
  47. QueryRowPartialCtx(ctx context.Context, v interface{}, args ...interface{}) error
  48. QueryRows(v interface{}, args ...interface{}) error
  49. QueryRowsCtx(ctx context.Context, v interface{}, args ...interface{}) error
  50. QueryRowsPartial(v interface{}, args ...interface{}) error
  51. QueryRowsPartialCtx(ctx context.Context, v interface{}, args ...interface{}) error
  52. }
  53. // thread-safe
  54. // Because CORBA doesn't support PREPARE, so we need to combine the
  55. // query arguments into one string and do underlying query without arguments
  56. commonSqlConn struct {
  57. connProv connProvider
  58. onError func(error)
  59. beginTx beginnable
  60. brk breaker.Breaker
  61. accept func(error) bool
  62. }
  63. connProvider func() (*sql.DB, error)
  64. sessionConn interface {
  65. Exec(query string, args ...interface{}) (sql.Result, error)
  66. ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
  67. Query(query string, args ...interface{}) (*sql.Rows, error)
  68. QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
  69. }
  70. statement struct {
  71. query string
  72. stmt *sql.Stmt
  73. }
  74. stmtConn interface {
  75. Exec(args ...interface{}) (sql.Result, error)
  76. ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error)
  77. Query(args ...interface{}) (*sql.Rows, error)
  78. QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error)
  79. }
  80. )
  81. // NewSqlConn returns a SqlConn with given driver name and datasource.
  82. func NewSqlConn(driverName, datasource string, opts ...SqlOption) SqlConn {
  83. conn := &commonSqlConn{
  84. connProv: func() (*sql.DB, error) {
  85. return getSqlConn(driverName, datasource)
  86. },
  87. onError: func(err error) {
  88. logInstanceError(datasource, err)
  89. },
  90. beginTx: begin,
  91. brk: breaker.NewBreaker(),
  92. }
  93. for _, opt := range opts {
  94. opt(conn)
  95. }
  96. return conn
  97. }
  98. // NewSqlConnFromDB returns a SqlConn with the given sql.DB.
  99. // Use it with caution, it's provided for other ORM to interact with.
  100. func NewSqlConnFromDB(db *sql.DB, opts ...SqlOption) SqlConn {
  101. conn := &commonSqlConn{
  102. connProv: func() (*sql.DB, error) {
  103. return db, nil
  104. },
  105. onError: func(err error) {
  106. logx.Errorf("Error on getting sql instance: %v", err)
  107. },
  108. beginTx: begin,
  109. brk: breaker.NewBreaker(),
  110. }
  111. for _, opt := range opts {
  112. opt(conn)
  113. }
  114. return conn
  115. }
  116. func (db *commonSqlConn) Exec(q string, args ...interface{}) (result sql.Result, err error) {
  117. return db.ExecCtx(context.Background(), q, args...)
  118. }
  119. func (db *commonSqlConn) ExecCtx(ctx context.Context, q string, args ...interface{}) (
  120. result sql.Result, err error) {
  121. ctx, span := startSpan(ctx, "Exec")
  122. defer func() {
  123. endSpan(span, err)
  124. }()
  125. err = db.brk.DoWithAcceptable(func() error {
  126. var conn *sql.DB
  127. conn, err = db.connProv()
  128. if err != nil {
  129. db.onError(err)
  130. return err
  131. }
  132. result, err = exec(ctx, conn, q, args...)
  133. return err
  134. }, db.acceptable)
  135. return
  136. }
  137. func (db *commonSqlConn) Prepare(query string) (stmt StmtSession, err error) {
  138. return db.PrepareCtx(context.Background(), query)
  139. }
  140. func (db *commonSqlConn) PrepareCtx(ctx context.Context, query string) (stmt StmtSession, err error) {
  141. ctx, span := startSpan(ctx, "Prepare")
  142. defer func() {
  143. endSpan(span, err)
  144. }()
  145. err = db.brk.DoWithAcceptable(func() error {
  146. var conn *sql.DB
  147. conn, err = db.connProv()
  148. if err != nil {
  149. db.onError(err)
  150. return err
  151. }
  152. st, err := conn.PrepareContext(ctx, query)
  153. if err != nil {
  154. return err
  155. }
  156. stmt = statement{
  157. query: query,
  158. stmt: st,
  159. }
  160. return nil
  161. }, db.acceptable)
  162. return
  163. }
  164. func (db *commonSqlConn) QueryRow(v interface{}, q string, args ...interface{}) error {
  165. return db.QueryRowCtx(context.Background(), v, q, args...)
  166. }
  167. func (db *commonSqlConn) QueryRowCtx(ctx context.Context, v interface{}, q string,
  168. args ...interface{}) (err error) {
  169. ctx, span := startSpan(ctx, "QueryRow")
  170. defer func() {
  171. endSpan(span, err)
  172. }()
  173. return db.queryRows(ctx, func(rows *sql.Rows) error {
  174. return unmarshalRow(v, rows, true)
  175. }, q, args...)
  176. }
  177. func (db *commonSqlConn) QueryRowPartial(v interface{}, q string, args ...interface{}) error {
  178. return db.QueryRowPartialCtx(context.Background(), v, q, args...)
  179. }
  180. func (db *commonSqlConn) QueryRowPartialCtx(ctx context.Context, v interface{},
  181. q string, args ...interface{}) (err error) {
  182. ctx, span := startSpan(ctx, "QueryRowPartial")
  183. defer func() {
  184. endSpan(span, err)
  185. }()
  186. return db.queryRows(ctx, func(rows *sql.Rows) error {
  187. return unmarshalRow(v, rows, false)
  188. }, q, args...)
  189. }
  190. func (db *commonSqlConn) QueryRows(v interface{}, q string, args ...interface{}) error {
  191. return db.QueryRowsCtx(context.Background(), v, q, args...)
  192. }
  193. func (db *commonSqlConn) QueryRowsCtx(ctx context.Context, v interface{}, q string,
  194. args ...interface{}) (err error) {
  195. ctx, span := startSpan(ctx, "QueryRows")
  196. defer func() {
  197. endSpan(span, err)
  198. }()
  199. return db.queryRows(ctx, func(rows *sql.Rows) error {
  200. return unmarshalRows(v, rows, true)
  201. }, q, args...)
  202. }
  203. func (db *commonSqlConn) QueryRowsPartial(v interface{}, q string, args ...interface{}) error {
  204. return db.QueryRowsPartialCtx(context.Background(), v, q, args...)
  205. }
  206. func (db *commonSqlConn) QueryRowsPartialCtx(ctx context.Context, v interface{},
  207. q string, args ...interface{}) (err error) {
  208. ctx, span := startSpan(ctx, "QueryRowsPartial")
  209. defer func() {
  210. endSpan(span, err)
  211. }()
  212. return db.queryRows(ctx, func(rows *sql.Rows) error {
  213. return unmarshalRows(v, rows, false)
  214. }, q, args...)
  215. }
  216. func (db *commonSqlConn) RawDB() (*sql.DB, error) {
  217. return db.connProv()
  218. }
  219. func (db *commonSqlConn) Transact(fn func(Session) error) error {
  220. return db.TransactCtx(context.Background(), func(_ context.Context, session Session) error {
  221. return fn(session)
  222. })
  223. }
  224. func (db *commonSqlConn) TransactCtx(ctx context.Context, fn func(context.Context, Session) error) (err error) {
  225. ctx, span := startSpan(ctx, "Transact")
  226. defer func() {
  227. endSpan(span, err)
  228. }()
  229. return db.brk.DoWithAcceptable(func() error {
  230. return transact(ctx, db, db.beginTx, fn)
  231. }, db.acceptable)
  232. }
  233. func (db *commonSqlConn) acceptable(err error) bool {
  234. ok := err == nil || err == sql.ErrNoRows || err == sql.ErrTxDone || err == context.Canceled
  235. if db.accept == nil {
  236. return ok
  237. }
  238. return ok || db.accept(err)
  239. }
  240. func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows) error,
  241. q string, args ...interface{}) (err error) {
  242. var qerr error
  243. return db.brk.DoWithAcceptable(func() error {
  244. conn, err := db.connProv()
  245. if err != nil {
  246. db.onError(err)
  247. return err
  248. }
  249. return query(ctx, conn, func(rows *sql.Rows) error {
  250. qerr = scanner(rows)
  251. return qerr
  252. }, q, args...)
  253. }, func(err error) bool {
  254. return qerr == err || db.acceptable(err)
  255. })
  256. }
  257. func (s statement) Close() error {
  258. return s.stmt.Close()
  259. }
  260. func (s statement) Exec(args ...interface{}) (sql.Result, error) {
  261. return s.ExecCtx(context.Background(), args...)
  262. }
  263. func (s statement) ExecCtx(ctx context.Context, args ...interface{}) (result sql.Result, err error) {
  264. ctx, span := startSpan(ctx, "Exec")
  265. defer func() {
  266. endSpan(span, err)
  267. }()
  268. return execStmt(ctx, s.stmt, s.query, args...)
  269. }
  270. func (s statement) QueryRow(v interface{}, args ...interface{}) error {
  271. return s.QueryRowCtx(context.Background(), v, args...)
  272. }
  273. func (s statement) QueryRowCtx(ctx context.Context, v interface{}, args ...interface{}) (err error) {
  274. ctx, span := startSpan(ctx, "QueryRow")
  275. defer func() {
  276. endSpan(span, err)
  277. }()
  278. return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
  279. return unmarshalRow(v, rows, true)
  280. }, s.query, args...)
  281. }
  282. func (s statement) QueryRowPartial(v interface{}, args ...interface{}) error {
  283. return s.QueryRowPartialCtx(context.Background(), v, args...)
  284. }
  285. func (s statement) QueryRowPartialCtx(ctx context.Context, v interface{}, args ...interface{}) (err error) {
  286. ctx, span := startSpan(ctx, "QueryRowPartial")
  287. defer func() {
  288. endSpan(span, err)
  289. }()
  290. return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
  291. return unmarshalRow(v, rows, false)
  292. }, s.query, args...)
  293. }
  294. func (s statement) QueryRows(v interface{}, args ...interface{}) error {
  295. return s.QueryRowsCtx(context.Background(), v, args...)
  296. }
  297. func (s statement) QueryRowsCtx(ctx context.Context, v interface{}, args ...interface{}) (err error) {
  298. ctx, span := startSpan(ctx, "QueryRows")
  299. defer func() {
  300. endSpan(span, err)
  301. }()
  302. return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
  303. return unmarshalRows(v, rows, true)
  304. }, s.query, args...)
  305. }
  306. func (s statement) QueryRowsPartial(v interface{}, args ...interface{}) error {
  307. return s.QueryRowsPartialCtx(context.Background(), v, args...)
  308. }
  309. func (s statement) QueryRowsPartialCtx(ctx context.Context, v interface{}, args ...interface{}) (err error) {
  310. ctx, span := startSpan(ctx, "QueryRowsPartial")
  311. defer func() {
  312. endSpan(span, err)
  313. }()
  314. return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
  315. return unmarshalRows(v, rows, false)
  316. }, s.query, args...)
  317. }