Skip to content
Merged
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
7 changes: 4 additions & 3 deletions flavor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package sqlbuilder
import (
"errors"
"fmt"
"strings"
)

// Supported flavors.
Expand Down Expand Up @@ -168,11 +169,11 @@ func (f Flavor) NewCTEQueryBuilder() *CTEQueryBuilder {
func (f Flavor) Quote(name string) string {
switch f {
case MySQL, ClickHouse, Doris:
return fmt.Sprintf("`%s`", name)
return fmt.Sprintf("`%s`", strings.ReplaceAll(name, "`", "``"))
case PostgreSQL, SQLServer, SQLite, Presto, Oracle, Informix:
return fmt.Sprintf(`"%s"`, name)
return fmt.Sprintf(`"%s"`, strings.ReplaceAll(name, `"`, `""`))
case CQL:
return fmt.Sprintf("'%s'", name)
return fmt.Sprintf("'%s'", strings.ReplaceAll(name, "'", "''"))
}

return name
Expand Down
27 changes: 27 additions & 0 deletions flavor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ func TestFlavor(t *testing.T) {
}
}

func TestFlavorQuoteEscapesDelimiters(t *testing.T) {
cases := []struct {
flavor Flavor
input string
want string
}{
{MySQL, "name` DESC", "`name`` DESC`"},
{ClickHouse, "name` DESC", "`name`` DESC`"},
{Doris, "name` DESC", "`name`` DESC`"},
{PostgreSQL, `name" DESC`, `"name"" DESC"`},
{SQLServer, `name" DESC`, `"name"" DESC"`},
{SQLite, `name" DESC`, `"name"" DESC"`},
{Presto, `name" DESC`, `"name"" DESC"`},
{Oracle, `name" DESC`, `"name"" DESC"`},
{Informix, `name" DESC`, `"name"" DESC"`},
{CQL, "name' DESC", "'name'' DESC'"},
}

for _, tc := range cases {
t.Run(tc.flavor.String(), func(t *testing.T) {
if got := tc.flavor.Quote(tc.input); got != tc.want {
t.Fatalf("Quote(%q) = %q, want %q", tc.input, got, tc.want)
}
})
}
}

func ExampleFlavor() {
// Create a flavored builder.
sb := PostgreSQL.NewSelectBuilder()
Expand Down
Loading