1 Star 0 Fork 0

hbgo/dameng

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ze.go 23.41 KB
一键复制 编辑 原始数据 按行查看 历史
VioletFlame 提交于 2024-11-04 16:09 . init
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package godm
import (
"context"
"database/sql/driver"
"fmt"
"gitee.com/hbgo/dameng/godm/util"
"io"
"reflect"
"strconv"
"time"
)
type logFilter struct{}
func (filter *logFilter) DmDriverOpen(filterChain *filterChain, d *DmDriver, dsn string) (ret *DmConnection, err error) {
var logRecord = d.logInfo.logRecord.Reset()
logRecord.Set(d, "open", dsn)
defer func() {
filter.doLog(logRecord)
}()
ret, err = filterChain.DmDriverOpen(d, dsn)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmDriverOpenConnector(filterChain *filterChain, d *DmDriver, dsn string) (ret *DmConnector, err error) {
var logRecord = d.logInfo.logRecord.Reset()
logRecord.Set(d, "openConnector", dsn)
defer func() {
filter.doLog(logRecord)
}()
ret, err = filterChain.DmDriverOpenConnector(d, dsn)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmConnectorConnect(filterChain *filterChain, c *DmConnector, ctx context.Context) (ret *DmConnection, err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "connect")
defer func() {
filter.doLog(logRecord)
}()
ret, err = filterChain.DmConnectorConnect(c, ctx)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmConnectorDriver(filterChain *filterChain, c *DmConnector) (ret *DmDriver) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "driver")
defer func() {
filter.doLog(logRecord)
}()
ret = filterChain.DmConnectorDriver(c)
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmConnectionBegin(filterChain *filterChain, c *DmConnection) (ret *DmConnection, err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "begin")
defer func() {
filter.doLog(logRecord)
}()
ret, err = filterChain.DmConnectionBegin(c)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmConnectionBeginTx(filterChain *filterChain, c *DmConnection, ctx context.Context, opts driver.TxOptions) (ret *DmConnection, err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "beginTx", opts)
defer func() {
filter.doLog(logRecord)
}()
ret, err = filterChain.DmConnectionBeginTx(c, ctx, opts)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmConnectionCommit(filterChain *filterChain, c *DmConnection) (err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "commit")
defer func() {
filter.doLog(logRecord)
}()
err = filterChain.DmConnectionCommit(c)
if err != nil {
logRecord.SetError(err)
return
}
return
}
func (filter *logFilter) DmConnectionRollback(filterChain *filterChain, c *DmConnection) (err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "rollback")
defer func() {
filter.doLog(logRecord)
}()
err = filterChain.DmConnectionRollback(c)
if err != nil {
logRecord.SetError(err)
return
}
return
}
func (filter *logFilter) DmConnectionClose(filterChain *filterChain, c *DmConnection) (err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "close")
defer func() {
filter.doLog(logRecord)
}()
err = filterChain.DmConnectionClose(c)
if err != nil {
logRecord.SetError(err)
return
}
return
}
func (filter *logFilter) DmConnectionPing(filterChain *filterChain, c *DmConnection, ctx context.Context) (err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "ping")
defer func() {
filter.doLog(logRecord)
}()
err = filterChain.DmConnectionPing(c, ctx)
if err != nil {
logRecord.SetError(err)
return
}
return
}
func (filter *logFilter) DmConnectionExec(filterChain *filterChain, c *DmConnection, query string, args []driver.Value) (ret *DmResult, err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "exec", convertParams2(args)...)
defer func() {
filter.executeAfter(c.logInfo, logRecord)
filter.doLog(logRecord)
}()
logRecord.SetSql(query, true)
filter.executeBefore(c.logInfo)
ret, err = filterChain.DmConnectionExec(c, query, args)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmConnectionExecContext(filterChain *filterChain, c *DmConnection, ctx context.Context, query string, args []driver.NamedValue) (ret *DmResult, err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "execCtx", convertParams1(args)...)
defer func() {
filter.executeAfter(c.logInfo, logRecord)
filter.doLog(logRecord)
}()
logRecord.SetSql(query, true)
filter.executeBefore(c.logInfo)
ret, err = filterChain.DmConnectionExecContext(c, ctx, query, args)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmConnectionQuery(filterChain *filterChain, c *DmConnection, query string, args []driver.Value) (ret *DmRows, err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "query", convertParams2(args)...)
defer func() {
filter.executeAfter(c.logInfo, logRecord)
filter.doLog(logRecord)
}()
logRecord.SetSql(query, true)
filter.executeBefore(c.logInfo)
ret, err = filterChain.DmConnectionQuery(c, query, args)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmConnectionQueryContext(filterChain *filterChain, c *DmConnection, ctx context.Context, query string, args []driver.NamedValue) (ret *DmRows, err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "queryCtx", convertParams1(args)...)
defer func() {
filter.executeAfter(c.logInfo, logRecord)
filter.doLog(logRecord)
}()
logRecord.SetSql(query, true)
filter.executeBefore(c.logInfo)
ret, err = filterChain.DmConnectionQueryContext(c, ctx, query, args)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmConnectionPrepare(filterChain *filterChain, c *DmConnection, query string) (ret *DmStatement, err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "prepare", query)
defer func() {
filter.doLog(logRecord)
}()
logRecord.SetSql(query, false)
ret, err = filterChain.DmConnectionPrepare(c, query)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmConnectionPrepareContext(filterChain *filterChain, c *DmConnection, ctx context.Context, query string) (ret *DmStatement, err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "prepareCtx", query)
defer func() {
filter.doLog(logRecord)
}()
logRecord.SetSql(query, false)
ret, err = filterChain.DmConnectionPrepareContext(c, ctx, query)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmConnectionResetSession(filterChain *filterChain, c *DmConnection, ctx context.Context) (err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "resetSession")
defer func() {
filter.doLog(logRecord)
}()
err = filterChain.DmConnectionResetSession(c, ctx)
if err != nil {
logRecord.SetError(err)
return
}
return
}
func (filter *logFilter) DmConnectionCheckNamedValue(filterChain *filterChain, c *DmConnection, nv *driver.NamedValue) (err error) {
var logRecord = c.logInfo.logRecord.Reset()
logRecord.Set(c, "checkNamedValue", nv.Value)
defer func() {
filter.doLog(logRecord)
}()
err = filterChain.DmConnectionCheckNamedValue(c, nv)
if err != nil {
logRecord.SetError(err)
return
}
return
}
func (filter *logFilter) DmStatementClose(filterChain *filterChain, s *DmStatement) (err error) {
var logRecord = s.logInfo.logRecord.Reset()
logRecord.Set(s, "close")
defer func() {
filter.doLog(logRecord)
}()
err = filterChain.DmStatementClose(s)
if err != nil {
logRecord.SetError(err)
return
}
return
}
func (filter *logFilter) DmStatementNumInput(filterChain *filterChain, s *DmStatement) (ret int) {
var logRecord = s.logInfo.logRecord.Reset()
logRecord.Set(s, "numInput")
defer func() {
filter.doLog(logRecord)
}()
ret = filterChain.DmStatementNumInput(s)
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmStatementExec(filterChain *filterChain, s *DmStatement, args []driver.Value) (ret *DmResult, err error) {
var logRecord = s.logInfo.logRecord.Reset()
logRecord.Set(s, "exec", convertParams2(args)...)
defer func() {
filter.executeAfter(s.logInfo, logRecord)
filter.doLog(logRecord)
}()
logRecord.SetSql(s.nativeSql, true)
filter.executeBefore(s.logInfo)
ret, err = filterChain.DmStatementExec(s, args)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmStatementExecContext(filterChain *filterChain, s *DmStatement, ctx context.Context, args []driver.NamedValue) (ret *DmResult, err error) {
var logRecord = s.logInfo.logRecord.Reset()
logRecord.Set(s, "execCtx", convertParams1(args)...)
defer func() {
filter.executeAfter(s.logInfo, logRecord)
filter.doLog(logRecord)
}()
logRecord.SetSql(s.nativeSql, true)
filter.executeBefore(s.logInfo)
ret, err = filterChain.DmStatementExecContext(s, ctx, args)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmStatementQuery(filterChain *filterChain, s *DmStatement, args []driver.Value) (ret *DmRows, err error) {
var logRecord = s.logInfo.logRecord.Reset()
logRecord.Set(s, "query", convertParams2(args)...)
defer func() {
filter.executeAfter(s.logInfo, logRecord)
filter.doLog(logRecord)
}()
logRecord.SetSql(s.nativeSql, true)
filter.executeBefore(s.logInfo)
ret, err = filterChain.DmStatementQuery(s, args)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmStatementQueryContext(filterChain *filterChain, s *DmStatement, ctx context.Context, args []driver.NamedValue) (ret *DmRows, err error) {
var logRecord = s.logInfo.logRecord.Reset()
logRecord.Set(s, "queryCtx", convertParams1(args)...)
defer func() {
filter.executeAfter(s.logInfo, logRecord)
filter.doLog(logRecord)
}()
logRecord.SetSql(s.nativeSql, true)
filter.executeBefore(s.logInfo)
ret, err = filterChain.DmStatementQueryContext(s, ctx, args)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmStatementCheckNamedValue(filterChain *filterChain, s *DmStatement, nv *driver.NamedValue) (err error) {
var logRecord = s.logInfo.logRecord.Reset()
logRecord.Set(s, "checkNamedValue", nv.Value)
defer func() {
filter.doLog(logRecord)
}()
err = filterChain.DmStatementCheckNamedValue(s, nv)
if err != nil {
logRecord.SetError(err)
return
}
return
}
func (filter *logFilter) DmResultLastInsertId(filterChain *filterChain, r *DmResult) (ret int64, err error) {
var logRecord = r.logInfo.logRecord.Reset()
logRecord.Set(r, "lastInsertId")
defer func() {
filter.doLog(logRecord)
}()
ret, err = filterChain.DmResultLastInsertId(r)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmResultRowsAffected(filterChain *filterChain, r *DmResult) (ret int64, err error) {
var logRecord = r.logInfo.logRecord.Reset()
logRecord.Set(r, "rowsAffected")
defer func() {
filter.doLog(logRecord)
}()
ret, err = filterChain.DmResultRowsAffected(r)
if err != nil {
logRecord.SetError(err)
return
}
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmRowsColumns(filterChain *filterChain, r *DmRows) (ret []string) {
var logRecord = r.logInfo.logRecord.Reset()
logRecord.Set(r, "columns")
defer func() {
filter.doLog(logRecord)
}()
ret = filterChain.DmRowsColumns(r)
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmRowsClose(filterChain *filterChain, r *DmRows) (err error) {
var logRecord = r.logInfo.logRecord.Reset()
logRecord.Set(r, "close")
defer func() {
filter.doLog(logRecord)
}()
err = filterChain.DmRowsClose(r)
if err != nil {
logRecord.SetError(err)
return
}
return
}
func (filter *logFilter) DmRowsNext(filterChain *filterChain, r *DmRows, dest []driver.Value) (err error) {
var logRecord = r.logInfo.logRecord.Reset()
logRecord.Set(r, "next", convertParams2(dest)...)
defer func() {
filter.doLog(logRecord)
}()
err = filterChain.DmRowsNext(r, dest)
if err != nil && err != io.EOF {
logRecord.SetError(err)
return
}
return
}
func (filter *logFilter) DmRowsHasNextResultSet(filterChain *filterChain, r *DmRows) (ret bool) {
var logRecord = r.logInfo.logRecord.Reset()
logRecord.Set(r, "hasNextResultSet")
defer func() {
filter.doLog(logRecord)
}()
ret = filterChain.DmRowsHasNextResultSet(r)
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmRowsNextResultSet(filterChain *filterChain, r *DmRows) (err error) {
var logRecord = r.logInfo.logRecord.Reset()
logRecord.Set(r, "nextResultSet")
defer func() {
filter.doLog(logRecord)
}()
err = filterChain.DmRowsNextResultSet(r)
if err != nil && err != io.EOF {
logRecord.SetError(err)
return
}
return
}
func (filter *logFilter) DmRowsColumnTypeScanType(filterChain *filterChain, r *DmRows, index int) (ret reflect.Type) {
var logRecord = r.logInfo.logRecord.Reset()
logRecord.Set(r, "columnTypeScanType", index)
defer func() {
filter.doLog(logRecord)
}()
ret = filterChain.DmRowsColumnTypeScanType(r, index)
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmRowsColumnTypeDatabaseTypeName(filterChain *filterChain, r *DmRows, index int) (ret string) {
var logRecord = r.logInfo.logRecord.Reset()
logRecord.Set(r, "columnTypeDatabaseTypeName", index)
defer func() {
filter.doLog(logRecord)
}()
ret = filterChain.DmRowsColumnTypeDatabaseTypeName(r, index)
logRecord.SetReturnValue(ret)
return
}
func (filter *logFilter) DmRowsColumnTypeLength(filterChain *filterChain, r *DmRows, index int) (length int64, ok bool) {
var logRecord = r.logInfo.logRecord.Reset()
logRecord.Set(r, "columnTypeLength", index)
defer func() {
filter.doLog(logRecord)
}()
length, ok = filterChain.DmRowsColumnTypeLength(r, index)
if ok {
logRecord.SetReturnValue(length)
} else {
logRecord.SetReturnValue(-1)
}
return
}
func (filter *logFilter) DmRowsColumnTypeNullable(filterChain *filterChain, r *DmRows, index int) (nullable, ok bool) {
var logRecord = r.logInfo.logRecord.Reset()
logRecord.Set(r, "columnTypeNullable", index)
defer func() {
filter.doLog(logRecord)
}()
nullable, ok = filterChain.DmRowsColumnTypeNullable(r, index)
if ok {
logRecord.SetReturnValue(nullable)
} else {
logRecord.SetReturnValue(false)
}
return
}
func (filter *logFilter) DmRowsColumnTypePrecisionScale(filterChain *filterChain, r *DmRows, index int) (precision, scale int64, ok bool) {
var logRecord = r.logInfo.logRecord.Reset()
logRecord.Set(r, "columnTypePrecisionScale", index)
defer func() {
filter.doLog(logRecord)
}()
precision, scale, ok = filterChain.DmRowsColumnTypePrecisionScale(r, index)
if ok {
logRecord.SetReturnValue(strconv.FormatInt(precision, 10) + "&" + strconv.FormatInt(scale, 10))
} else {
logRecord.SetReturnValue("-1&-1")
}
return
}
func (filter *logFilter) executeBefore(logInfo *logInfo) {
if LogFilterLogger.IsSqlEnabled() {
logInfo.lastExecuteStartNano = time.Now()
}
}
func (filter *logFilter) executeAfter(logInfo *logInfo, record *LogRecord) {
if LogFilterLogger.IsSqlEnabled() {
record.SetUsedTime(time.Since(logInfo.lastExecuteStartNano))
}
}
func (filter *logFilter) doLog(record *LogRecord) {
if record == nil {
return
}
if record.GetError() != nil {
LogFilterLogger.ErrorWithErr(record.ToString(), record.GetError())
} else if record.GetSql() != "" && LogFilterLogger.IsSqlEnabled() {
LogFilterLogger.Sql(record.ToString())
} else {
LogFilterLogger.Info(record.ToString())
}
}
/************************************************************************************************************/
type Logger struct {
}
var LogFilterLogger = &Logger{}
var ConnLogger = &Logger{}
var AccessLogger = &Logger{}
func (logger Logger) IsDebugEnabled() bool {
return LogLevel >= LOG_DEBUG
}
func (logger Logger) IsErrorEnabled() bool {
return LogLevel >= LOG_ERROR
}
func (logger Logger) IsInfoEnabled() bool {
return LogLevel >= LOG_INFO
}
func (logger Logger) IsWarnEnabled() bool {
return LogLevel >= LOG_WARN
}
func (logger Logger) IsSqlEnabled() bool {
return LogLevel >= LOG_SQL
}
func (logger Logger) Debug(msg string) {
if logger.IsDebugEnabled() {
logger.println(logger.formatHead("DEBUG") + msg)
}
}
func (logger Logger) DebugWithErr(msg string, err error) {
if logger.IsDebugEnabled() {
if e, ok := err.(*DmError); ok {
logger.println(logger.formatHead("DEBUG") + msg + util.LINE_SEPARATOR + e.Stack())
} else {
logger.println(logger.formatHead("DEBUG") + msg + util.LINE_SEPARATOR + err.Error())
}
}
}
func (logger Logger) Info(msg string) {
if logger.IsInfoEnabled() {
logger.println(logger.formatHead("INFO ") + msg)
}
}
func (logger Logger) Sql(msg string) {
if logger.IsSqlEnabled() {
logger.println(logger.formatHead("SQL ") + msg)
}
}
func (logger Logger) Warn(msg string) {
if logger.IsWarnEnabled() {
logger.println(logger.formatHead("WARN ") + msg)
}
}
func (logger Logger) ErrorWithErr(msg string, err error) {
//if e, ok := err.(*DmError); ok {
// logger.println(logger.formatHead("ERROR") + msg + util.LINE_SEPARATOR + e.Stack())
//} else {
logger.println(logger.formatHead("ERROR") + msg + util.LINE_SEPARATOR + err.Error())
//}
}
// TODO: 获取goroutine objId
func (logger Logger) formatHead(head string) string {
// return "[" + head + " - " + StringUtil.formatTime() + "] tid:" + Thread.currentThread().getId();
return "[" + head + " - " + util.StringUtil.FormatTime() + "]"
}
func (logger Logger) println(msg string) {
goMap["log"].(*logWriter).WriteLine(msg)
}
/*************************************************************************************************/
func formatSource(source interface{}) string {
if source == nil {
return ""
}
var str string
switch src := source.(type) {
case string:
str += src
case *DmDriver:
str += formatDriver(src)
case *DmConnector:
str += formatContor(src)
case *DmConnection:
str += formatConn(src)
case *DmStatement:
str += formatConn(src.dmConn) + ", "
str += formatStmt(src)
case *DmResult:
str += formatConn(src.dmStmt.dmConn) + ", "
str += formatStmt(src.dmStmt) + ", "
str += formatRs(src)
case *DmRows:
str += formatConn(src.CurrentRows.dmStmt.dmConn) + ", "
str += formatStmt(src.CurrentRows.dmStmt) + ", "
str += formatRows(src)
default:
str += reflect.TypeOf(src).String() + "@" + reflect.ValueOf(src).Addr().String()
}
return str
}
func formatDriver(driver *DmDriver) string {
if driver != nil && driver.logInfo != nil {
return "driver-" + strconv.FormatInt(driver.getID(), 10)
}
return "driver-nil"
}
func formatContor(contor *DmConnector) string {
if contor != nil && contor.logInfo != nil {
return "contor-" + strconv.FormatInt(contor.getID(), 10)
}
return "contor-nil"
}
func formatConn(conn *DmConnection) string {
if conn != nil && conn.logInfo != nil {
return "conn-0x" + strconv.FormatInt(conn.SessionID, 16)
}
return "conn-nil"
}
func formatStmt(stmt *DmStatement) string {
if stmt != nil && stmt.logInfo != nil {
return "stmt-" + strconv.Itoa(int(stmt.id))
}
return "stmt-nil"
}
func formatRs(result *DmResult) string {
if result != nil && result.logInfo != nil {
return "rs-" + strconv.FormatInt(result.getID(), 10)
}
return "rs-nil"
}
func formatRows(rows *DmRows) string {
if rows != nil && rows.logInfo != nil {
return "rows-" + strconv.FormatInt(rows.getID(), 10)
}
return "rows-nil"
}
func formatTrace(source string, sql string, method string, returnValue interface{}, params ...interface{}) string {
var str string
if source != "" {
str += "{ " + source + " } "
}
str += method + "("
var paramStartIndex = 0
if params != nil && len(params) > paramStartIndex {
for i := paramStartIndex; i < len(params); i++ {
if i != paramStartIndex {
str += ", "
}
if params[i] != nil {
str += reflect.TypeOf(params[i]).String()
} else {
str += "nil"
}
}
}
str += ")"
if returnValue != nil {
str += ": " + formatReturn(returnValue)
}
str += "; "
if params != nil && len(params) > paramStartIndex {
str += "[PARAMS]: "
for i := paramStartIndex; i < len(params); i++ {
if i != 0 {
str += ", "
}
//if s, ok := params[i].(driver.NamedValue); ok {
// str += fmt.Sprintf("%v", s.Value)
//} else {
str += fmt.Sprintf("%v", params[i])
//}
}
str += "; "
}
if sql != "" {
str += "[SQL]: " + sql + "; "
}
return str
}
func formatReturn(returnObj interface{}) string {
var str string
switch o := returnObj.(type) {
case *DmConnection:
str = formatConn(o)
case *DmStatement:
str = formatStmt(o)
case *DmResult:
str = formatRs(o)
case *DmRows:
str = formatRows(o)
case string:
str = `"` + o + `"`
case nullData:
str = "nil"
default:
str = "unknown"
}
return str
}
func formatUsedTime(duration time.Duration) string {
return "[USED TIME]: " + duration.String()
}
/************************************************************************************************************/
type nullData struct{}
var null = nullData{}
type LogRecord struct {
source string
method string
params []interface{}
returnValue interface{}
e error
usedTime time.Duration
sql string
logSql bool // 是否需要记录sql(exec,query等需要在日志中记录sql语句)
}
func (record *LogRecord) Reset() *LogRecord {
record.source = ""
record.method = ""
record.params = nil
record.returnValue = nil
record.e = nil
record.usedTime = 0
record.sql = ""
record.logSql = false
return record
}
func (record *LogRecord) Set(source interface{}, method string, params ...interface{}) {
record.source = formatSource(source)
record.method = method
record.params = params
}
func (record *LogRecord) SetReturnValue(retValue interface{}) {
if retValue == nil {
record.returnValue = null
} else {
record.returnValue = retValue
}
}
func (record *LogRecord) GetReturnValue() interface{} {
return record.returnValue
}
func (record *LogRecord) SetSql(sql string, logSql bool) {
record.sql = sql
record.logSql = logSql
}
func (record *LogRecord) GetSql() string {
return record.sql
}
func (record *LogRecord) SetUsedTime(usedTime time.Duration) {
record.usedTime = usedTime
}
func (record *LogRecord) GetUsedTime() time.Duration {
return record.usedTime
}
func (record *LogRecord) SetError(err error) {
record.e = err
}
func (record *LogRecord) GetError() error {
return record.e
}
func (record *LogRecord) ToString() string {
var sql string
if record.logSql && record.sql != "" {
sql = record.sql
}
var str string
str += formatTrace(record.source, sql, record.method, record.returnValue, record.params...)
if record.usedTime > 0 {
str += formatUsedTime(record.usedTime)
}
return str
}
func convertParams1(args []driver.NamedValue) []interface{} {
tmp := make([]interface{}, len(args))
for i := 0; i < len(tmp); i++ {
tmp[i] = args[i].Value
}
return tmp
}
func convertParams2(args []driver.Value) []interface{} {
tmp := make([]interface{}, len(args))
for i := 0; i < len(tmp); i++ {
tmp[i] = args[i]
}
return tmp
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/hbgo/dameng.git
git@gitee.com:hbgo/dameng.git
hbgo
dameng
dameng
v1.0.2

搜索帮助