mirror of https://github.com/go-gorm/gorm.git
Merge pull request #1256 from fobispo-link/master
Issue #1249 fix - replacing $ placeholders in order when logging
This commit is contained in:
commit
2c025b25fe
13
logger.go
13
logger.go
|
@ -13,7 +13,8 @@ import (
|
|||
|
||||
var (
|
||||
defaultLogger = Logger{log.New(os.Stdout, "\r\n", 0)}
|
||||
sqlRegexp = regexp.MustCompile(`(\$\d+)|\?`)
|
||||
sqlRegexp = regexp.MustCompile(`\?`)
|
||||
numericPlaceHolderRegexp = regexp.MustCompile(`\$\d+`)
|
||||
)
|
||||
|
||||
type logger interface {
|
||||
|
@ -71,6 +72,15 @@ func (logger Logger) Print(values ...interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
// differentiate between $n placeholders or else treat like ?
|
||||
if numericPlaceHolderRegexp.MatchString(values[3].(string)) {
|
||||
sql = values[3].(string)
|
||||
for index, value := range formattedValues {
|
||||
placeholder := fmt.Sprintf(`\$%d`, index+1)
|
||||
subre := regexp.MustCompile(placeholder)
|
||||
sql = subre.ReplaceAllString(sql, value)
|
||||
}
|
||||
} else {
|
||||
var formattedValuesLength = len(formattedValues)
|
||||
for index, value := range sqlRegexp.Split(values[3].(string), -1) {
|
||||
sql += value
|
||||
|
@ -78,6 +88,7 @@ func (logger Logger) Print(values ...interface{}) {
|
|||
sql += formattedValues[index]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
messages = append(messages, sql)
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue