MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / EncodeSQLStringWithFlags

Function EncodeSQLStringWithFlags

pkg/sql/lexbase/encode.go:154–209  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

152// will not be wrapped in quotes if the strings contains no special
153// characters.
154func EncodeSQLStringWithFlags(buf *bytes.Buffer, in string, flags EncodeFlags) {
155 // See http://www.postgresql.org/docs/9.4/static/sql-syntax-lexical.html
156 start := 0
157 skipEscape := flags.HasFlags(EncSkipEscapeString)
158 escapedString := false
159 bareStrings := flags.HasFlags(EncBareStrings)
160 // Loop through each unicode code point.
161 for i, r := range in {
162 if i < start {
163 continue
164 }
165 ch := byte(r)
166 if r >= minPrintableChar && r <= maxPrintableChar {
167 if mustQuoteMap[ch] {
168 // We have to quote this string - ignore bareStrings setting
169 bareStrings = false
170 }
171 if !stringencoding.NeedEscape(ch) && ch != '\'' {
172 continue
173 }
174 }
175
176 // If non-ASCII characters are allowed,
177 // skip escaping and write the original UTF-8 character.
178 if r > maxPrintableChar && skipEscape {
179 continue
180 }
181
182 if !skipEscape && !escapedString {
183 buf.WriteString("e'") // begin e'xxx' string
184 escapedString = true
185 }
186 buf.WriteString(in[start:i])
187
188 ln := utf8.RuneLen(r)
189 if ln < 0 {
190 start = i + 1
191 } else {
192 start = i + ln
193 }
194 // If we skip escaping, we don't write the slash char before the
195 // quote char, so we will write the quote char directly.
196 stringencoding.EncodeEscapedChar(buf, in, r, ch, i, '\'', !skipEscape)
197 }
198
199 quote := !escapedString && !bareStrings && !skipEscape
200 if quote {
201 buf.WriteByte('\'') // begin 'xxx' string if nothing was escaped
202 }
203 if start < len(in) {
204 buf.WriteString(in[start:])
205 }
206 if !skipEscape && (escapedString || quote) {
207 buf.WriteByte('\'')
208 }
209}
210
211// EncodeSQLBytes encodes the SQL byte array in 'in' to buf, to a

Callers 15

FormatMethod · 0.92
FormatMethod · 0.92
FormatMethod · 0.92
FormatMethod · 0.92
FormatMethod · 0.92
FormatMethod · 0.92
FormatMethod · 0.92
FormatMethod · 0.92
FormatMethod · 0.92
FormatMethod · 0.92
FormatMethod · 0.92
FormatMethod · 0.92

Calls 3

NeedEscapeFunction · 0.92
EncodeEscapedCharFunction · 0.92
HasFlagsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…