Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mysql quoting issues #829

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@ func (commonDialect) Lock(fn func() error) error {
return fn()
}

func (commonDialect) Quote(key string) string {
parts := strings.Split(key, ".")
func quoteIdentifiers(s, quote string) string {
parts := strings.Split(s, ".")

for i, part := range parts {
part = strings.Trim(part, `"`)
part = strings.Trim(part, quote)
part = strings.TrimSpace(part)

parts[i] = fmt.Sprintf(`"%v"`, part)
parts[i] = quote + part + quote
}

return strings.Join(parts, ".")

}

func (commonDialect) Quote(key string) string {
return quoteIdentifiers(key, `"`)
}

func genericCreate(c *Connection, model *Model, cols columns.Columns, quoter quotable) error {
Expand Down
2 changes: 1 addition & 1 deletion dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (m *mysql) DefaultDriver() string {
}

func (mysql) Quote(key string) string {
return fmt.Sprintf("`%s`", key)
return quoteIdentifiers(key, "`")
}

func (m *mysql) Details() *ConnectionDetails {
Expand Down
9 changes: 9 additions & 0 deletions dialect_mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,12 @@ func (s *MySQLSuite) Test_MySQL_DDL_Schema() {
err = PDB.Dialect.DumpSchema(f)
r.Error(err)
}

func Test_MySQL_Quote(t *testing.T) {
r := require.New(t)

m := &mysql{}
r.Equal("`table_name`", m.Quote("table_name"))
r.Equal("`schema`.`table_name`", m.Quote("schema.table_name"))
r.Equal("`schema`.`table_name`", m.Quote(m.Quote("schema.table_name")))
}