Ejemplo n.º 1
0
func (s String) encodeSql(b encoding2.BinaryWriter) {
	if s.isUtf8 {
		writebyte(b, '\'')
		rawBytes := s.raw()
		for i, ch := range rawBytes {
			if encodedChar := SqlEncodeMap[ch]; encodedChar == DONTESCAPE {
				writebyte(b, ch)
			} else if i < len(rawBytes)-1 && '\\' == ch && ('%' == rawBytes[i+1] || '_' == rawBytes[i+1]) {
				// Don't escape '\' specifically in the constructions '\%' or
				// '\_', because those are special to how the RHS of LIKE
				// clauses are escaped. See the notes following table 9.1 in
				// http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
				writebyte(b, ch)
			} else {
				writebyte(b, '\\')
				writebyte(b, encodedChar)
			}
		}
		writebyte(b, '\'')
	} else {
		b.Write([]byte("X'"))
		encoding2.HexEncodeToWriter(b, s.raw())
		writebyte(b, '\'')
	}
}
Ejemplo n.º 2
0
func (s String) encodeSql(b encoding2.BinaryWriter) {
	if s.isUtf8 {
		writebyte(b, '\'')
		for _, ch := range s.raw() {
			if encodedChar := SqlEncodeMap[ch]; encodedChar == DONTESCAPE {
				writebyte(b, ch)
			} else {
				writebyte(b, '\\')
				writebyte(b, encodedChar)
			}
		}
		writebyte(b, '\'')
	} else {
		b.Write([]byte("X'"))
		encoding2.HexEncodeToWriter(b, s.raw())
		writebyte(b, '\'')
	}
}