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)
| 101 | // will not be wrapped in quotes if the strings contains no special |
| 102 | // characters. |
| 103 | func EncodeSQLStringWithFlags(buf *bytes.Buffer, in string, flags EncodeFlags) { |
| 104 | // See http://www.postgresql.org/docs/9.4/static/sql-syntax-lexical.html |
| 105 | start := 0 |
| 106 | escapedString := false |
| 107 | bareStrings := flags.HasFlags(EncBareStrings) |
| 108 | // Loop through each unicode code point. |
| 109 | for i, r := range in { |
| 110 | if i < start { |
| 111 | continue |
| 112 | } |
| 113 | ch := byte(r) |
| 114 | if r >= 0x20 && r < 0x7F { |
| 115 | if mustQuoteMap[ch] { |
| 116 | // We have to quote this string - ignore bareStrings setting |
| 117 | bareStrings = false |
| 118 | } |
| 119 | if !stringencoding.NeedEscape(ch) && ch != '\'' { |
| 120 | continue |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if !escapedString { |
| 125 | buf.WriteString("e'") // begin e'xxx' string |
| 126 | escapedString = true |
| 127 | } |
| 128 | buf.WriteString(in[start:i]) |
| 129 | ln := utf8.RuneLen(r) |
| 130 | if ln < 0 { |
| 131 | start = i + 1 |
| 132 | } else { |
| 133 | start = i + ln |
| 134 | } |
| 135 | stringencoding.EncodeEscapedChar(buf, in, r, ch, i, '\'') |
| 136 | } |
| 137 | |
| 138 | quote := !escapedString && !bareStrings |
| 139 | if quote { |
| 140 | buf.WriteByte('\'') // begin 'xxx' string if nothing was escaped |
| 141 | } |
| 142 | if start < len(in) { |
| 143 | buf.WriteString(in[start:]) |
| 144 | } |
| 145 | if escapedString || quote { |
| 146 | buf.WriteByte('\'') |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // EncodeUnrestrictedSQLIdent writes the identifier in s to buf. |
| 151 | // The identifier is only quoted if the flags don't tell otherwise and |
no test coverage detected