Skip to content

Commit

Permalink
输出日志sql执行的详细日志日期
Browse files Browse the repository at this point in the history
  • Loading branch information
lucky-lee committed Sep 12, 2019
1 parent 4436644 commit e1969e4
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions gDb/gMysql/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,41 @@ package gMysql

import (
"database/sql"
"github.com/lucky-lee/gutil/gJson"
"github.com/lucky-lee/gutil/gLog"
)

//exec easy
func ExecEasy(sqlStr string, db *sql.DB, args ...interface{}) bool {
gLog.Sql("execEasySql", sqlStr) //add sql log

result, err := db.Exec(sqlStr, args...)
return exec(result, err)
return exec(sqlStr, db, args...)
}

//exec easy and return last id
func ExecEasyLastId(sqlStr string, db *sql.DB, args ...interface{}) int64 {
gLog.Sql("execEasyLastIdSql", sqlStr) //add sql log

result, err := db.Exec(sqlStr, args...)
return execLastId(result, err)
return execLastId(sqlStr, db, args...)
}

//exec
func exec(result sql.Result, err error) bool {
func exec(sqlStr string, db *sql.DB, args ...interface{}) bool {
result, err := db.Exec(sqlStr, args...)

if err != nil {
gLog.E("sqlErr", err)
gLog.E("sqlErrSql", sqlStr)
gLog.E("sqlErrParams", gJson.Encode(args))
return false
}

rowsAffected, err := result.RowsAffected()

if err != nil {
gLog.E("sqlErr", err)
gLog.E("sqlErrSql", sqlStr)
gLog.E("sqlErrParams", gJson.Encode(args))
return false
}

Expand All @@ -43,16 +48,22 @@ func exec(result sql.Result, err error) bool {
}

//exec and return last last id
func execLastId(result sql.Result, err error) int64 {
func execLastId(sqlStr string, db *sql.DB, args ...interface{}) int64 {
result, err := db.Exec(sqlStr, args...)

if err != nil {
gLog.E("sqlErr", err)
gLog.E("sqlErrSql", sqlStr)
gLog.E("sqlErrParams", gJson.Encode(args))
return 0
}

id, err := result.LastInsertId()

if err != nil {
gLog.E("sqlErr", err)
gLog.E("sqlErrSql", sqlStr)
gLog.E("sqlErrParams", gJson.Encode(args))
return 0
}

Expand Down

0 comments on commit e1969e4

Please sign in to comment.