github.com/shuguocloud/go-zero@v1.3.0/core/stores/sqlx/stmt.go (about)

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