Skip to content

Commit

Permalink
strings.replace -> strings.replaceAll (go-gorm#5095)
Browse files Browse the repository at this point in the history
Co-authored-by: huquan<[email protected]>
  • Loading branch information
hu-quan-er authored Feb 20, 2022
1 parent f3547e0 commit 664c5fb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions logger/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, a
case reflect.Bool:
vars[idx] = fmt.Sprintf("%t", reflectValue.Interface())
case reflect.String:
vars[idx] = escaper + strings.Replace(fmt.Sprintf("%v", v), escaper, "\\"+escaper, -1) + escaper
vars[idx] = escaper + strings.ReplaceAll(fmt.Sprintf("%v", v), escaper, "\\"+escaper) + escaper
default:
if v != nil && reflectValue.IsValid() && ((reflectValue.Kind() == reflect.Ptr && !reflectValue.IsNil()) || reflectValue.Kind() != reflect.Ptr) {
vars[idx] = escaper + strings.Replace(fmt.Sprintf("%v", v), escaper, "\\"+escaper, -1) + escaper
vars[idx] = escaper + strings.ReplaceAll(fmt.Sprintf("%v", v), escaper, "\\"+escaper) + escaper
} else {
vars[idx] = nullStr
}
Expand All @@ -94,7 +94,7 @@ func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, a
case float64, float32:
vars[idx] = fmt.Sprintf("%.6f", v)
case string:
vars[idx] = escaper + strings.Replace(v, escaper, "\\"+escaper, -1) + escaper
vars[idx] = escaper + strings.ReplaceAll(v, escaper, "\\"+escaper) + escaper
default:
rv := reflect.ValueOf(v)
if v == nil || !rv.IsValid() || rv.Kind() == reflect.Ptr && rv.IsNil() {
Expand All @@ -111,7 +111,7 @@ func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, a
return
}
}
vars[idx] = escaper + strings.Replace(fmt.Sprint(v), escaper, "\\"+escaper, -1) + escaper
vars[idx] = escaper + strings.ReplaceAll(fmt.Sprint(v), escaper, "\\"+escaper) + escaper
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion logger/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s ExampleStruct) Value() (driver.Value, error) {
}

func format(v []byte, escaper string) string {
return escaper + strings.Replace(string(v), escaper, "\\"+escaper, -1) + escaper
return escaper + strings.ReplaceAll(string(v), escaper, "\\"+escaper) + escaper
}

func TestExplainSQL(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion schema/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (ns NamingStrategy) toDBName(name string) string {
}

func (ns NamingStrategy) toSchemaName(name string) string {
result := strings.Replace(strings.Title(strings.Replace(name, "_", " ", -1)), " ", "", -1)
result := strings.ReplaceAll(strings.Title(strings.ReplaceAll(name, "_", " ")), " ", "")
for _, initialism := range commonInitialisms {
result = regexp.MustCompile(strings.Title(strings.ToLower(initialism))+"([A-Z]|$|_)").ReplaceAllString(result, initialism+"$1")
}
Expand Down
8 changes: 4 additions & 4 deletions tests/sql_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,16 +460,16 @@ func assertEqualSQL(t *testing.T, expected string, actually string) {

func replaceQuoteInSQL(sql string) string {
// convert single quote into double quote
sql = strings.Replace(sql, `'`, `"`, -1)
sql = strings.ReplaceAll(sql, `'`, `"`)

// convert dialect speical quote into double quote
switch DB.Dialector.Name() {
case "postgres":
sql = strings.Replace(sql, `"`, `"`, -1)
sql = strings.ReplaceAll(sql, `"`, `"`)
case "mysql", "sqlite":
sql = strings.Replace(sql, "`", `"`, -1)
sql = strings.ReplaceAll(sql, "`", `"`)
case "sqlserver":
sql = strings.Replace(sql, `'`, `"`, -1)
sql = strings.ReplaceAll(sql, `'`, `"`)
}

return sql
Expand Down

0 comments on commit 664c5fb

Please sign in to comment.