photoprism/pkg/sanitize/sql.go
Michael Mayer 9d110e8b80 Search: Improve album, albums, lens, and camera filters #1994 #2079
Camera and lens can now also be searched by name. Escaping and parsing
of albums has been improved so that albums whose names start with and/or
contain numbers will be found.
2022-03-24 18:30:59 +01:00

40 lines
717 B
Go

package sanitize
import (
"bytes"
)
// sqlSpecialBytes contains special bytes to escape in SQL search queries.
var sqlSpecialBytes = []byte{34, 39, 92, 95}
// SqlString escapes a string for use in an SQL query.
func SqlString(s string) string {
var i int
for i = 0; i < len(s); i++ {
if bytes.Contains(sqlSpecialBytes, []byte{s[i]}) {
break
}
}
// No special characters found, return original string.
if i >= len(s) {
return s
}
b := make([]byte, 2*len(s)-i)
copy(b, s[:i])
j := i
for ; i < len(s); i++ {
if s[i] < 31 {
// Ignore control chars.
continue
}
if bytes.Contains(sqlSpecialBytes, []byte{s[i]}) {
b[j] = '\\'
j++
}
b[j] = s[i]
j++
}
return string(b[:j])
}