EncodeSQLStringWithFlags writes a string literal to buf. All unicode and non-printable characters are escaped. flags controls the output format: if encodeBareString is set, the output string will not be wrapped in quotes if the strings contains no special characters.
(buf *bytes.Buffer, in string, flags EncodeFlags)
| 88 | // will not be wrapped in quotes if the strings contains no special |
| 89 | // characters. |
| 90 | func EncodeSQLStringWithFlags(buf *bytes.Buffer, in string, flags EncodeFlags) { |
| 91 | // See http://www.postgresql.org/docs/9.4/static/sql-syntax-lexical.html |
| 92 | start := 0 |
| 93 | escapedString := false |
| 94 | bareStrings := flags.HasFlags(EncBareStrings) |
| 95 | // Loop through each unicode code point. |
| 96 | for i, r := range in { |
| 97 | if i < start { |
| 98 | continue |
| 99 | } |
| 100 | ch := byte(r) |
| 101 | if r >= 0x20 && r < 0x7F { |
| 102 | if mustQuoteMap[ch] { |
| 103 | // We have to quote this string - ignore bareStrings setting |
| 104 | bareStrings = false |
| 105 | } |
| 106 | if !stringencoding.NeedEscape(ch) && ch != '\'' { |
| 107 | continue |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if !escapedString { |
| 112 | buf.WriteString("e'") // begin e'xxx' string |
| 113 | escapedString = true |
| 114 | } |
| 115 | buf.WriteString(in[start:i]) |
| 116 | ln := utf8.RuneLen(r) |
| 117 | if ln < 0 { |
| 118 | start = i + 1 |
| 119 | } else { |
| 120 | start = i + ln |
| 121 | } |
| 122 | stringencoding.EncodeEscapedChar(buf, in, r, ch, i, '\'') |
| 123 | } |
| 124 | |
| 125 | quote := !escapedString && !bareStrings |
| 126 | if quote { |
| 127 | buf.WriteByte('\'') // begin 'xxx' string if nothing was escaped |
| 128 | } |
| 129 | if start < len(in) { |
| 130 | buf.WriteString(in[start:]) |
| 131 | } |
| 132 | if escapedString || quote { |
| 133 | buf.WriteByte('\'') |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // EncodeUnrestrictedSQLIdent writes the identifier in s to buf. |
| 138 | // The identifier is only quoted if the flags don't tell otherwise and |
no test coverage detected
searching dependent graphs…