stmt.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package sqlx
  2. import (
  3. "context"
  4. "database/sql"
  5. "time"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "github.com/zeromicro/go-zero/core/syncx"
  8. "github.com/zeromicro/go-zero/core/timex"
  9. )
  10. const defaultSlowThreshold = time.Millisecond * 500
  11. var (
  12. slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
  13. logSql = syncx.ForAtomicBool(true)
  14. logSlowSql = syncx.ForAtomicBool(true)
  15. )
  16. // DisableLog disables logging of sql statements, includes info and slow logs.
  17. func DisableLog() {
  18. logSql.Set(false)
  19. logSlowSql.Set(false)
  20. }
  21. // DisableStmtLog disables info logging of sql statements, but keeps slow logs.
  22. func DisableStmtLog() {
  23. logSql.Set(false)
  24. }
  25. // SetSlowThreshold sets the slow threshold.
  26. func SetSlowThreshold(threshold time.Duration) {
  27. slowThreshold.Set(threshold)
  28. }
  29. func exec(ctx context.Context, conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
  30. guard := newGuard("exec")
  31. if err := guard.start(q, args...); err != nil {
  32. return nil, err
  33. }
  34. result, err := conn.ExecContext(ctx, q, args...)
  35. guard.finish(ctx, err)
  36. return result, err
  37. }
  38. func execStmt(ctx context.Context, conn stmtConn, q string, args ...interface{}) (sql.Result, error) {
  39. guard := newGuard("execStmt")
  40. if err := guard.start(q, args...); err != nil {
  41. return nil, err
  42. }
  43. result, err := conn.ExecContext(ctx, args...)
  44. guard.finish(ctx, err)
  45. return result, err
  46. }
  47. func query(ctx context.Context, conn sessionConn, scanner func(*sql.Rows) error,
  48. q string, args ...interface{}) error {
  49. guard := newGuard("query")
  50. if err := guard.start(q, args...); err != nil {
  51. return err
  52. }
  53. rows, err := conn.QueryContext(ctx, q, args...)
  54. guard.finish(ctx, err)
  55. if err != nil {
  56. return err
  57. }
  58. defer rows.Close()
  59. return scanner(rows)
  60. }
  61. func queryStmt(ctx context.Context, conn stmtConn, scanner func(*sql.Rows) error,
  62. q string, args ...interface{}) error {
  63. guard := newGuard("queryStmt")
  64. if err := guard.start(q, args...); err != nil {
  65. return err
  66. }
  67. rows, err := conn.QueryContext(ctx, args...)
  68. guard.finish(ctx, err)
  69. if err != nil {
  70. return err
  71. }
  72. defer rows.Close()
  73. return scanner(rows)
  74. }
  75. type (
  76. sqlGuard interface {
  77. start(q string, args ...interface{}) error
  78. finish(ctx context.Context, err error)
  79. }
  80. nilGuard struct{}
  81. realSqlGuard struct {
  82. command string
  83. stmt string
  84. startTime time.Duration
  85. }
  86. )
  87. func newGuard(command string) sqlGuard {
  88. if logSql.True() || logSlowSql.True() {
  89. return &realSqlGuard{
  90. command: command,
  91. }
  92. }
  93. return nilGuard{}
  94. }
  95. func (n nilGuard) start(_ string, _ ...interface{}) error {
  96. return nil
  97. }
  98. func (n nilGuard) finish(_ context.Context, _ error) {
  99. }
  100. func (e *realSqlGuard) finish(ctx context.Context, err error) {
  101. duration := timex.Since(e.startTime)
  102. if duration > slowThreshold.Load() {
  103. logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] %s: slowcall - %s", e.command, e.stmt)
  104. } else if logSql.True() {
  105. logx.WithContext(ctx).WithDuration(duration).Infof("sql %s: %s", e.command, e.stmt)
  106. }
  107. if err != nil {
  108. logSqlError(ctx, e.stmt, err)
  109. }
  110. }
  111. func (e *realSqlGuard) start(q string, args ...interface{}) error {
  112. stmt, err := format(q, args...)
  113. if err != nil {
  114. return err
  115. }
  116. e.stmt = stmt
  117. e.startTime = timex.Now()
  118. return nil
  119. }