-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split
stringutil.Wrap
into two functions (#538)
- Loading branch information
1 parent
4153191
commit efec8fd
Showing
8 changed files
with
77 additions
and
66 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,15 @@ | ||
package sql | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/artie-labs/transfer/lib/stringutil" | ||
) | ||
|
||
// QuoteLiteral wraps a string with single quotes so that it can be used in a SQL query. | ||
// If there are backslashes in the string, then they will be escaped to [\\]. | ||
// After escaping backslashes, any remaining single quotes will be replaced with [\']. | ||
func QuoteLiteral(value string) string { | ||
return fmt.Sprintf("'%s'", strings.ReplaceAll(stringutil.EscapeBackslashes(value), "'", `\'`)) | ||
} |
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,40 @@ | ||
package sql | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestQuoteLiteral(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
colVal string | ||
expected string | ||
}{ | ||
{ | ||
name: "string", | ||
colVal: "hello", | ||
expected: "'hello'", | ||
}, | ||
{ | ||
name: "string that requires escaping", | ||
colVal: "bobby o'reilly", | ||
expected: `'bobby o\'reilly'`, | ||
}, | ||
{ | ||
name: "string with line breaks", | ||
colVal: "line1 \n line 2", | ||
expected: "'line1 \n line 2'", | ||
}, | ||
{ | ||
name: "string with existing backslash", | ||
colVal: `hello \ there \ hh`, | ||
expected: `'hello \\ there \\ hh'`, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
assert.Equal(t, testCase.expected, QuoteLiteral(testCase.colVal), testCase.name) | ||
} | ||
} |
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
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