-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move SQL escape functions to the Postgres package
These are specific to Postgres. Add tests and remove unused functions.
- Loading branch information
Showing
5 changed files
with
44 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2021 - 2024 Crunchy Data Solutions, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package postgres | ||
|
||
import "strings" | ||
|
||
// escapeLiteral is called by QuoteLiteral to add backslashes before special | ||
// characters of the "escape" string syntax. Double quote marks to escape them | ||
// regardless of the "backslash_quote" parameter. | ||
var escapeLiteral = strings.NewReplacer(`'`, `''`, `\`, `\\`).Replace | ||
|
||
// QuoteLiteral escapes v so it can be safely used as a literal (or constant) | ||
// in an SQL statement. | ||
func QuoteLiteral(v string) string { | ||
// Use the "escape" syntax to ensure that backslashes behave consistently regardless | ||
// of the "standard_conforming_strings" parameter. Include a space before so | ||
// the "E" cannot change the meaning of an adjacent SQL keyword or identifier. | ||
// - https://www.postgresql.org/docs/current/sql-syntax-lexical.html | ||
return ` E'` + escapeLiteral(v) + `'` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2021 - 2024 Crunchy Data Solutions, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package postgres | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestQuoteLiteral(t *testing.T) { | ||
assert.Equal(t, QuoteLiteral(``), ` E''`) | ||
assert.Equal(t, QuoteLiteral(`ab"cd\ef'gh`), ` E'ab"cd\\ef''gh'`) | ||
} |
This file was deleted.
Oops, something went wrong.