|
|
|
@ -2,7 +2,6 @@ package sqlx
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/core/logx"
|
|
|
|
@ -12,10 +11,14 @@ import (
|
|
|
|
|
const slowThreshold = time.Millisecond * 500
|
|
|
|
|
|
|
|
|
|
func exec(conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
|
|
|
|
|
stmt, err := format(q, args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startTime := timex.Now()
|
|
|
|
|
result, err := conn.Exec(q, args...)
|
|
|
|
|
duration := timex.Since(startTime)
|
|
|
|
|
stmt := formatForPrint(q, args)
|
|
|
|
|
if duration > slowThreshold {
|
|
|
|
|
logx.WithDuration(duration).Slowf("[SQL] exec: slowcall - %s", stmt)
|
|
|
|
|
} else {
|
|
|
|
@ -28,11 +31,15 @@ func exec(conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func execStmt(conn stmtConn, args ...interface{}) (sql.Result, error) {
|
|
|
|
|
func execStmt(conn stmtConn, q string, args ...interface{}) (sql.Result, error) {
|
|
|
|
|
stmt, err := format(q, args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startTime := timex.Now()
|
|
|
|
|
result, err := conn.Exec(args...)
|
|
|
|
|
duration := timex.Since(startTime)
|
|
|
|
|
stmt := fmt.Sprint(args...)
|
|
|
|
|
if duration > slowThreshold {
|
|
|
|
|
logx.WithDuration(duration).Slowf("[SQL] execStmt: slowcall - %s", stmt)
|
|
|
|
|
} else {
|
|
|
|
@ -46,10 +53,14 @@ func execStmt(conn stmtConn, args ...interface{}) (sql.Result, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func query(conn sessionConn, scanner func(*sql.Rows) error, q string, args ...interface{}) error {
|
|
|
|
|
stmt, err := format(q, args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startTime := timex.Now()
|
|
|
|
|
rows, err := conn.Query(q, args...)
|
|
|
|
|
duration := timex.Since(startTime)
|
|
|
|
|
stmt := fmt.Sprint(args...)
|
|
|
|
|
if duration > slowThreshold {
|
|
|
|
|
logx.WithDuration(duration).Slowf("[SQL] query: slowcall - %s", stmt)
|
|
|
|
|
} else {
|
|
|
|
@ -64,8 +75,12 @@ func query(conn sessionConn, scanner func(*sql.Rows) error, q string, args ...in
|
|
|
|
|
return scanner(rows)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func queryStmt(conn stmtConn, scanner func(*sql.Rows) error, args ...interface{}) error {
|
|
|
|
|
stmt := fmt.Sprint(args...)
|
|
|
|
|
func queryStmt(conn stmtConn, scanner func(*sql.Rows) error, q string, args ...interface{}) error {
|
|
|
|
|
stmt, err := format(q, args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startTime := timex.Now()
|
|
|
|
|
rows, err := conn.Query(args...)
|
|
|
|
|
duration := timex.Since(startTime)
|
|
|
|
|