stmt.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package sqlx
  2. import (
  3. "database/sql"
  4. "time"
  5. "github.com/tal-tech/go-zero/core/logx"
  6. "github.com/tal-tech/go-zero/core/syncx"
  7. "github.com/tal-tech/go-zero/core/timex"
  8. )
  9. const defaultSlowThreshold = time.Millisecond * 500
  10. var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
  11. // SetSlowThreshold sets the slow threshold.
  12. func SetSlowThreshold(threshold time.Duration) {
  13. slowThreshold.Set(threshold)
  14. }
  15. func exec(conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
  16. stmt, err := format(q, args...)
  17. if err != nil {
  18. return nil, err
  19. }
  20. startTime := timex.Now()
  21. result, err := conn.Exec(q, args...)
  22. duration := timex.Since(startTime)
  23. if duration > slowThreshold.Load() {
  24. logx.WithDuration(duration).Slowf("[SQL] exec: slowcall - %s", stmt)
  25. } else {
  26. logx.WithDuration(duration).Infof("sql exec: %s", stmt)
  27. }
  28. if err != nil {
  29. logSqlError(stmt, err)
  30. }
  31. return result, err
  32. }
  33. func execStmt(conn stmtConn, q string, args ...interface{}) (sql.Result, error) {
  34. stmt, err := format(q, args...)
  35. if err != nil {
  36. return nil, err
  37. }
  38. startTime := timex.Now()
  39. result, err := conn.Exec(args...)
  40. duration := timex.Since(startTime)
  41. if duration > slowThreshold.Load() {
  42. logx.WithDuration(duration).Slowf("[SQL] execStmt: slowcall - %s", stmt)
  43. } else {
  44. logx.WithDuration(duration).Infof("sql execStmt: %s", stmt)
  45. }
  46. if err != nil {
  47. logSqlError(stmt, err)
  48. }
  49. return result, err
  50. }
  51. func query(conn sessionConn, scanner func(*sql.Rows) error, q string, args ...interface{}) error {
  52. stmt, err := format(q, args...)
  53. if err != nil {
  54. return err
  55. }
  56. startTime := timex.Now()
  57. rows, err := conn.Query(q, args...)
  58. duration := timex.Since(startTime)
  59. if duration > slowThreshold.Load() {
  60. logx.WithDuration(duration).Slowf("[SQL] query: slowcall - %s", stmt)
  61. } else {
  62. logx.WithDuration(duration).Infof("sql query: %s", stmt)
  63. }
  64. if err != nil {
  65. logSqlError(stmt, err)
  66. return err
  67. }
  68. defer rows.Close()
  69. return scanner(rows)
  70. }
  71. func queryStmt(conn stmtConn, scanner func(*sql.Rows) error, q string, args ...interface{}) error {
  72. stmt, err := format(q, args...)
  73. if err != nil {
  74. return err
  75. }
  76. startTime := timex.Now()
  77. rows, err := conn.Query(args...)
  78. duration := timex.Since(startTime)
  79. if duration > slowThreshold.Load() {
  80. logx.WithDuration(duration).Slowf("[SQL] queryStmt: slowcall - %s", stmt)
  81. } else {
  82. logx.WithDuration(duration).Infof("sql queryStmt: %s", stmt)
  83. }
  84. if err != nil {
  85. logSqlError(stmt, err)
  86. return err
  87. }
  88. defer rows.Close()
  89. return scanner(rows)
  90. }