stmt.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
  12. // SetSlowThreshold sets the slow threshold.
  13. func SetSlowThreshold(threshold time.Duration) {
  14. slowThreshold.Set(threshold)
  15. }
  16. func exec(ctx context.Context, conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
  17. stmt, err := format(q, args...)
  18. if err != nil {
  19. return nil, err
  20. }
  21. startTime := timex.Now()
  22. result, err := conn.ExecContext(ctx, q, args...)
  23. duration := timex.Since(startTime)
  24. if duration > slowThreshold.Load() {
  25. logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] exec: slowcall - %s", stmt)
  26. } else {
  27. logx.WithContext(ctx).WithDuration(duration).Infof("sql exec: %s", stmt)
  28. }
  29. if err != nil {
  30. logSqlError(ctx, stmt, err)
  31. }
  32. return result, err
  33. }
  34. func execStmt(ctx context.Context, conn stmtConn, q string, args ...interface{}) (sql.Result, error) {
  35. stmt, err := format(q, args...)
  36. if err != nil {
  37. return nil, err
  38. }
  39. startTime := timex.Now()
  40. result, err := conn.ExecContext(ctx, args...)
  41. duration := timex.Since(startTime)
  42. if duration > slowThreshold.Load() {
  43. logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] execStmt: slowcall - %s", stmt)
  44. } else {
  45. logx.WithContext(ctx).WithDuration(duration).Infof("sql execStmt: %s", stmt)
  46. }
  47. if err != nil {
  48. logSqlError(ctx, stmt, err)
  49. }
  50. return result, err
  51. }
  52. func query(ctx context.Context, conn sessionConn, scanner func(*sql.Rows) error,
  53. q string, args ...interface{}) error {
  54. stmt, err := format(q, args...)
  55. if err != nil {
  56. return err
  57. }
  58. startTime := timex.Now()
  59. rows, err := conn.QueryContext(ctx, q, args...)
  60. duration := timex.Since(startTime)
  61. if duration > slowThreshold.Load() {
  62. logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] query: slowcall - %s", stmt)
  63. } else {
  64. logx.WithContext(ctx).WithDuration(duration).Infof("sql query: %s", stmt)
  65. }
  66. if err != nil {
  67. logSqlError(ctx, stmt, err)
  68. return err
  69. }
  70. defer rows.Close()
  71. return scanner(rows)
  72. }
  73. func queryStmt(ctx context.Context, conn stmtConn, scanner func(*sql.Rows) error,
  74. q string, args ...interface{}) error {
  75. stmt, err := format(q, args...)
  76. if err != nil {
  77. return err
  78. }
  79. startTime := timex.Now()
  80. rows, err := conn.QueryContext(ctx, args...)
  81. duration := timex.Since(startTime)
  82. if duration > slowThreshold.Load() {
  83. logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] queryStmt: slowcall - %s", stmt)
  84. } else {
  85. logx.WithContext(ctx).WithDuration(duration).Infof("sql queryStmt: %s", stmt)
  86. }
  87. if err != nil {
  88. logSqlError(ctx, stmt, err)
  89. return err
  90. }
  91. defer rows.Close()
  92. return scanner(rows)
  93. }