GraphQL API and utilities for the rpdata project
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.
 
 

56 lines
915 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, "\"") {
token = token[1:]
startQuotes = true
}
if strings.HasSuffix(token, "\"") {
token = token[:len(token)-1]
clearQuotes = true
}
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, " ")
}