Skip to content

Commit

Permalink
Improve dialect_common Quote function (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
tatang26 authored and stanislas-m committed Oct 15, 2019
1 parent 8c9b756 commit 88a44ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ func (commonDialect) Lock(fn func() error) error {
}

func (commonDialect) Quote(key string) string {
return fmt.Sprintf(`"%s"`, key)
parts := strings.Split(key, ".")

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

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

return strings.Join(parts, ".")
}

func genericCreate(s store, model *Model, cols columns.Columns, quoter quotable) error {
Expand Down
9 changes: 9 additions & 0 deletions dialect_postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,12 @@ func Test_PostgreSQL_Connection_String_Failure(t *testing.T) {
r.Error(err)
r.Equal("postgres", cd.Dialect)
}

func Test_PostgreSQL_Quotable(t *testing.T) {
r := require.New(t)
p := postgresql{}

r.Equal(`"table_name"`, p.Quote("table_name"))
r.Equal(`"schema"."table_name"`, p.Quote("schema.table_name"))
r.Equal(`"schema"."table name"`, p.Quote(`"schema"."table name"`))
}

0 comments on commit 88a44ed

Please sign in to comment.