Skip to content

Commit

Permalink
refactor: Regex description (#6507)
Browse files Browse the repository at this point in the history
* Mirror cleanup

* Regex description

---------

Co-authored-by: Ignat Belousov <ignatbelousov@Ignats-MacBook-Pro.local>
  • Loading branch information
fayvori and Ignat Belousov committed Aug 7, 2023
1 parent f473761 commit 3c34bc2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions logger/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ func isPrintable(s string) bool {
return true
}

// A list of Go types that should be converted to SQL primitives
var convertibleTypes = []reflect.Type{reflect.TypeOf(time.Time{}), reflect.TypeOf(false), reflect.TypeOf([]byte{})}

// RegEx matches only numeric values
var numericPlaceholderRe = regexp.MustCompile(`\$\d+\$`)

// ExplainSQL generate SQL string with given parameters, the generated SQL is expected to be used in logger, execute it might introduce a SQL injection vulnerability
Expand Down
1 change: 0 additions & 1 deletion logger/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ func TestExplainSQL(t *testing.T) {
Vars: []interface{}{"jinzhu", 1, 0.1753607109, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, &js, &es},
Result: fmt.Sprintf(`create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values ("jinzhu", 1, 0.1753607109, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.\"com", "admin", "pass", %v, %v)`, format(jsVal, `"`), format(esVal, `"`)),
},

}

for idx, r := range results {
Expand Down
9 changes: 9 additions & 0 deletions migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ import (
"gorm.io/gorm/schema"
)

// This regular expression seeks to find a sequence of digits (\d+) among zero or more non-digit characters (\D*),
// with a possible trailing non-digit character (\D?).

// For example, values that can pass this regular expression are:
// - "123"
// - "abc456"
// -"%$#@789"
var regFullDataType = regexp.MustCompile(`\D*(\d+)\D?`)

// TODO:? Create const vars for raw sql queries ?

// Migrator m struct
type Migrator struct {
Config
Expand Down
6 changes: 3 additions & 3 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ require (
github.com/jackc/pgx/v5 v5.4.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.4.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/text v0.11.0 // indirect
github.com/microsoft/go-mssqldb v1.5.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/text v0.12.0 // indirect
)

replace gorm.io/gorm => ../

0 comments on commit 3c34bc2

Please sign in to comment.