Skip to content

Commit

Permalink
fix: correctly escape single quotes in pg arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
nikklassen committed Oct 23, 2021
1 parent a2faf78 commit 3010847
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dialect/pgdialect/append.go
Expand Up @@ -286,7 +286,7 @@ func arrayAppendString(b []byte, s string) []byte {
case 0:
// ignore
case '\'':
b = append(b, "'''"...)
b = append(b, "''"...)
case '"':
b = append(b, '\\', '"')
case '\\':
Expand Down
8 changes: 8 additions & 0 deletions dialect/pgdialect/array_parser.go
Expand Up @@ -110,6 +110,14 @@ func (p *arrayParser) readSubstring() ([]byte, error) {
}
continue
}
if c == '\'' && next == '\'' {
p.buf = append(p.buf, next)
c, err = p.readByte()
if err != nil {
return nil, err
}
continue
}

p.buf = append(p.buf, c)
c = next
Expand Down
6 changes: 3 additions & 3 deletions dialect/pgdialect/array_parser_test.go
Expand Up @@ -13,9 +13,9 @@ func TestArrayParser(t *testing.T) {
{`{}`, []string{}},
{`{""}`, []string{""}},
{`{"\\"}`, []string{`\`}},
{`{"''"}`, []string{"''"}},
{`{{"''\"{}"}}`, []string{`{"''\"{}"}`}},
{`{"''\"{}"}`, []string{`''"{}`}},
{`{"''"}`, []string{"'"}},
{`{{"'\"{}"}}`, []string{`{"'\"{}"}`}},
{`{"'\"{}"}`, []string{`'"{}`}},

{"{1,2}", []string{"1", "2"}},
{"{1,NULL}", []string{"1", ""}},
Expand Down

0 comments on commit 3010847

Please sign in to comment.