Skip to content

Commit

Permalink
logPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
yinshuwei committed Jul 18, 2024
1 parent 6dca986 commit 1f4e8e5
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 49 deletions.
14 changes: 7 additions & 7 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func setValueConvert(isPtr bool, dest reflect.Value, value interface{}, destType
// convertAssign copies to dest the value in src, converting it if possible.
// An error is returned if the copy would result in loss of information.
// dest should be a pointer type.
func (o *osmBase) convertAssign(dest reflect.Value, src interface{}, destIsPtr bool, destType reflect.Type) error {
func (o *osmBase) convertAssign(logPrefix string, dest reflect.Value, src interface{}, destIsPtr bool, destType reflect.Type) error {
switch s := src.(type) {
case string:
switch destType.Kind() {
Expand Down Expand Up @@ -110,7 +110,7 @@ func (o *osmBase) convertAssign(dest reflect.Value, src interface{}, destIsPtr b
case reflect.Bool:
bv, err := driver.Bool.ConvertValue(src)
if err != nil {
o.options.WarnLogger.Log("convertAssign Bool error", map[string]string{"error": err.Error()})
o.options.WarnLogger.Log(logPrefix+"convertAssign Bool error", map[string]string{"error": err.Error()})
bv = false
}
setValue(destIsPtr, dest, bv.(bool), destType)
Expand All @@ -120,7 +120,7 @@ func (o *osmBase) convertAssign(dest reflect.Value, src interface{}, destIsPtr b
s = trimZeroDecimal(s)
i64, err := strconv.ParseInt(s, 10, destType.Bits())
if err != nil {
o.options.WarnLogger.Log("convertAssign Int error", map[string]string{"error": err.Error()})
o.options.WarnLogger.Log(logPrefix+"convertAssign Int error", map[string]string{"error": err.Error()})
}
dest.SetInt(i64)
return nil
Expand All @@ -129,15 +129,15 @@ func (o *osmBase) convertAssign(dest reflect.Value, src interface{}, destIsPtr b
s = trimZeroDecimal(s)
u64, err := strconv.ParseUint(s, 10, destType.Bits())
if err != nil {
o.options.WarnLogger.Log("convertAssign Uint error", map[string]string{"error": err.Error()})
o.options.WarnLogger.Log(logPrefix+"convertAssign Uint error", map[string]string{"error": err.Error()})
}
dest.SetUint(u64)
return nil
case reflect.Float32, reflect.Float64:
s := asString(src)
f64, err := strconv.ParseFloat(s, destType.Bits())
if err != nil {
o.options.WarnLogger.Log("convertAssign Float error", map[string]string{"error": err.Error()})
o.options.WarnLogger.Log(logPrefix+"convertAssign Float error", map[string]string{"error": err.Error()})
}
dest.SetFloat(f64)
return nil
Expand Down Expand Up @@ -170,14 +170,14 @@ func (o *osmBase) convertAssign(dest reflect.Value, src interface{}, destIsPtr b
t = t.Local()
setValue(destIsPtr, dest, t, destType)
} else {
o.options.WarnLogger.Log("convertAssign Time error", map[string]string{"error": err.Error()})
o.options.WarnLogger.Log(logPrefix+"convertAssign Time error", map[string]string{"error": err.Error()})
}
}
return nil
}
}

o.options.WarnLogger.Log(fmt.Sprintf("unsupported Scan, storing driver.Value type %T into type %T", src, dest), nil)
o.options.WarnLogger.Log(logPrefix+fmt.Sprintf("unsupported Scan, storing driver.Value type %T into type %T", src, dest), nil)
return nil
}

Expand Down
12 changes: 11 additions & 1 deletion osm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ package osm
import (
"database/sql"
"fmt"
"path"
"reflect"
"runtime"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -94,6 +97,13 @@ func (options *Options) tidy() {
// SlowLogDuration: 500 * time.Millisecond, // time.Duration
// })
func New(driverName, dataSource string, options Options) (*Osm, error) {
logPrefix := ""
_, file, lineNo, ok := runtime.Caller(1)
if ok {
fileName := path.Base(file)
logPrefix = fileName + ":" + strconv.Itoa(lineNo)
}

options.tidy()
osm := &Osm{
osmBase: osmBase{
Expand All @@ -120,7 +130,7 @@ func New(driverName, dataSource string, options Options) (*Osm, error) {
for {
err := db.Ping()
if err != nil {
osm.options.WarnLogger.Log("osm Ping fail", map[string]string{"error": err.Error()})
osm.options.WarnLogger.Log(logPrefix+"osm Ping fail", map[string]string{"error": err.Error()})
}
time.Sleep(time.Minute)
}
Expand Down
4 changes: 2 additions & 2 deletions result_kvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
)

func resultKvs(o *osmBase, id, sql string, sqlParams []interface{}, container interface{}) (int64, error) {
func resultKvs(logPrefix string, o *osmBase, id, sql string, sqlParams []interface{}, container interface{}) (int64, error) {
pointValue := reflect.ValueOf(container)
if pointValue.Kind() != reflect.Ptr {
return 0, fmt.Errorf("sql '%s' error : kvs类型Query,查询结果类型应为map的指针,而您传入的并不是指针", id)
Expand Down Expand Up @@ -40,7 +40,7 @@ func resultKvs(o *osmBase, id, sql string, sqlParams []interface{}, container in
reflect.New(elementTypes[0]).Elem(),
reflect.New(elementTypes[1]).Elem(),
}
err = o.scanRow(rows, isPtrs, elementTypes, objs)
err = o.scanRow(logPrefix, rows, isPtrs, elementTypes, objs)
if err != nil {
return 0, fmt.Errorf("sql '%s' error : %s", id, err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions result_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func checkDatas(datas interface{}) *reflect.Value {
}

// resultStrings 数据库结果读入到columns,和datas。columns为[]string,datas为[][]string。
func resultStrings(o *osmBase, id, sql string, sqlParams []interface{}, columnsContainer, datasContainer interface{}) (int64, error) {
func resultStrings(logPrefix string, o *osmBase, id, sql string, sqlParams []interface{}, columnsContainer, datasContainer interface{}) (int64, error) {
columnsValue := checkColumns(columnsContainer)
if columnsValue == nil {
return 0, fmt.Errorf("sql '%s' error : strings类型Query,查询结果类型第一个为[]string的指针,第二个为[][]string的指针", id)
Expand Down Expand Up @@ -84,7 +84,7 @@ func resultStrings(o *osmBase, id, sql string, sqlParams []interface{}, columnsC
for i := 0; i < columnsCount; i++ {
objs[i] = reflect.New(stringType).Elem()
}
err = o.scanRow(rows, isPtrs, elementTypes, objs)
err = o.scanRow(logPrefix, rows, isPtrs, elementTypes, objs)
if err != nil {
return 0, fmt.Errorf("sql '%s' error : %s", id, err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions result_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
)

func resultStruct(o *osmBase, id, sql string, sqlParams []interface{}, container interface{}) (int64, error) {
func resultStruct(logPrefix string, o *osmBase, id, sql string, sqlParams []interface{}, container interface{}) (int64, error) {
pointValue := reflect.ValueOf(container)
if pointValue.Kind() != reflect.Ptr {
return 0, fmt.Errorf("sql '%s' error : struct类型Query,查询结果类型应为struct的指针,而您传入的并不是指针", id)
Expand Down Expand Up @@ -52,7 +52,7 @@ func resultStruct(o *osmBase, id, sql string, sqlParams []interface{}, container
values[i] = reflect.ValueOf(&a).Elem()
}
}
err = o.scanRow(rows, isPtrs, elementTypes, values)
err = o.scanRow(logPrefix, rows, isPtrs, elementTypes, values)
if err != nil {
return 0, fmt.Errorf("sql '%s' error : %s", id, err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions result_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// resultStructs 数据库结果读入到struct切片中,struct可以是指针类型或非指针类型
func resultStructs(o *osmBase, id, sql string, sqlParams []interface{}, container interface{}) (int64, error) {
func resultStructs(logPrefix string, o *osmBase, id, sql string, sqlParams []interface{}, container interface{}) (int64, error) {
// 获得反射后结果的指针(这里应该是一个切片的指针)
pointValue := reflect.ValueOf(container)
if pointValue.Kind() != reflect.Ptr {
Expand Down Expand Up @@ -87,7 +87,7 @@ func resultStructs(o *osmBase, id, sql string, sqlParams []interface{}, containe
}
}
// 读取一行数据到成员实例切片中
err = o.scanRow(rows, isPtrs, elementTypes, values)
err = o.scanRow(logPrefix, rows, isPtrs, elementTypes, values)
if err != nil {
return 0, fmt.Errorf("sql '%s' error : %s", id, err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions result_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
)

func resultValue(o *osmBase, id, sql string, sqlParams []interface{}, containers []interface{}) (int64, error) {
func resultValue(logPrefix string, o *osmBase, id, sql string, sqlParams []interface{}, containers []interface{}) (int64, error) {
lenContainers := len(containers)
values := make([]reflect.Value, lenContainers)
elementTypes := make([]reflect.Type, lenContainers)
Expand Down Expand Up @@ -44,7 +44,7 @@ func resultValue(o *osmBase, id, sql string, sqlParams []interface{}, containers
return 0, fmt.Errorf("sql '%s' error : value类型Query,查询结果的长度与SQL的长度不一致", id)
}

err = o.scanRow(rows, isPtrs, elementTypes, values)
err = o.scanRow(logPrefix, rows, isPtrs, elementTypes, values)
if err != nil {
return 0, fmt.Errorf("sql '%s' error : %s", id, err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions result_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
)

func resultValues(o *osmBase, id, sql string, sqlParams []interface{}, containers []interface{}) (int64, error) {
func resultValues(logPrefix string, o *osmBase, id, sql string, sqlParams []interface{}, containers []interface{}) (int64, error) {
lenContainers := len(containers)
values := make([]reflect.Value, lenContainers)
elementTypes := make([]reflect.Type, lenContainers)
Expand Down Expand Up @@ -54,7 +54,7 @@ func resultValues(o *osmBase, id, sql string, sqlParams []interface{}, container
for i := 0; i < lenContainers; i++ {
objs[i] = reflect.New(elementTypes[i]).Elem()
}
err = o.scanRow(rows, isPtrs, elementTypes, objs)
err = o.scanRow(logPrefix, rows, isPtrs, elementTypes, objs)
if err != nil {
return 0, fmt.Errorf("sql '%s' error : %s", id, err.Error())
}
Expand Down
Loading

0 comments on commit 1f4e8e5

Please sign in to comment.