You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/stores/sqlx/stmt.go

111 lines
2.8 KiB
Go

4 years ago
package sqlx
import (
"context"
4 years ago
"database/sql"
"time"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/syncx"
"github.com/zeromicro/go-zero/core/timex"
4 years ago
)
const defaultSlowThreshold = time.Millisecond * 500
var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
// SetSlowThreshold sets the slow threshold.
func SetSlowThreshold(threshold time.Duration) {
slowThreshold.Set(threshold)
}
4 years ago
func exec(ctx context.Context, conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
stmt, err := format(q, args...)
if err != nil {
return nil, err
}
4 years ago
startTime := timex.Now()
result, err := conn.ExecContext(ctx, q, args...)
4 years ago
duration := timex.Since(startTime)
if duration > slowThreshold.Load() {
logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] exec: slowcall - %s", stmt)
4 years ago
} else {
logx.WithContext(ctx).WithDuration(duration).Infof("sql exec: %s", stmt)
4 years ago
}
if err != nil {
logSqlError(ctx, stmt, err)
4 years ago
}
return result, err
}
func execStmt(ctx context.Context, conn stmtConn, q string, args ...interface{}) (sql.Result, error) {
stmt, err := format(q, args...)
if err != nil {
return nil, err
}
4 years ago
startTime := timex.Now()
result, err := conn.ExecContext(ctx, args...)
4 years ago
duration := timex.Since(startTime)
if duration > slowThreshold.Load() {
logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] execStmt: slowcall - %s", stmt)
4 years ago
} else {
logx.WithContext(ctx).WithDuration(duration).Infof("sql execStmt: %s", stmt)
4 years ago
}
if err != nil {
logSqlError(ctx, stmt, err)
4 years ago
}
return result, err
}
func query(ctx context.Context, conn sessionConn, scanner func(*sql.Rows) error,
q string, args ...interface{}) error {
stmt, err := format(q, args...)
if err != nil {
return err
}
4 years ago
startTime := timex.Now()
rows, err := conn.QueryContext(ctx, q, args...)
4 years ago
duration := timex.Since(startTime)
if duration > slowThreshold.Load() {
logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] query: slowcall - %s", stmt)
4 years ago
} else {
logx.WithContext(ctx).WithDuration(duration).Infof("sql query: %s", stmt)
4 years ago
}
if err != nil {
logSqlError(ctx, stmt, err)
4 years ago
return err
}
defer rows.Close()
return scanner(rows)
}
func queryStmt(ctx context.Context, conn stmtConn, scanner func(*sql.Rows) error,
q string, args ...interface{}) error {
stmt, err := format(q, args...)
if err != nil {
return err
}
4 years ago
startTime := timex.Now()
rows, err := conn.QueryContext(ctx, args...)
4 years ago
duration := timex.Since(startTime)
if duration > slowThreshold.Load() {
logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] queryStmt: slowcall - %s", stmt)
4 years ago
} else {
logx.WithContext(ctx).WithDuration(duration).Infof("sql queryStmt: %s", stmt)
4 years ago
}
if err != nil {
logSqlError(ctx, stmt, err)
4 years ago
return err
}
defer rows.Close()
return scanner(rows)
}