You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1011 B
61 lines
1011 B
package postgres
|
|
|
|
import "strings"
|
|
|
|
// TSQueryFromSearch generates a TS query from a typical search string.
|
|
func TSQueryFromSearch(search string) string {
|
|
if strings.HasPrefix(search, "tsq:") {
|
|
return search[4:]
|
|
}
|
|
|
|
tokens := strings.Split(search, " ")
|
|
inQuotes := false
|
|
result := ""
|
|
|
|
for i, token := range tokens {
|
|
clearQuotes := false
|
|
startQuotes := false
|
|
|
|
if strings.HasPrefix(token, "\"") && len(token) > 1 {
|
|
startQuotes = true
|
|
}
|
|
if strings.HasSuffix(token, "\"") {
|
|
if !startQuotes {
|
|
clearQuotes = true
|
|
}
|
|
|
|
startQuotes = false
|
|
}
|
|
|
|
token := strings.Trim(token, ",.;:()\"")
|
|
if len(token) > 0 {
|
|
if startQuotes {
|
|
if i > 0 {
|
|
result += " & "
|
|
}
|
|
|
|
result += "("
|
|
} else if inQuotes {
|
|
result += "<->"
|
|
} else if i != 0 {
|
|
result += " & "
|
|
}
|
|
|
|
result += token
|
|
}
|
|
|
|
if startQuotes {
|
|
inQuotes = true
|
|
}
|
|
if clearQuotes {
|
|
inQuotes = false
|
|
result += ")"
|
|
}
|
|
}
|
|
|
|
if inQuotes {
|
|
return result
|
|
}
|
|
|
|
return strings.TrimLeft(result, " ")
|
|
}
|