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

  1. package postgres
  2. import "strings"
  3. // TSQueryFromSearch generates a TS query from a typical search string.
  4. func TSQueryFromSearch(search string) string {
  5. if strings.HasPrefix(search, "tsq:") {
  6. return search[4:]
  7. }
  8. tokens := strings.Split(search, " ")
  9. inQuotes := false
  10. result := ""
  11. for i, token := range tokens {
  12. clearQuotes := false
  13. startQuotes := false
  14. if strings.HasPrefix(token, "\"") {
  15. token = token[1:]
  16. startQuotes = true
  17. }
  18. if strings.HasSuffix(token, "\"") {
  19. token = token[:len(token)-1]
  20. clearQuotes = true
  21. }
  22. if startQuotes {
  23. if i > 0 {
  24. result += " & "
  25. }
  26. result += "("
  27. } else if inQuotes {
  28. result += "<->"
  29. } else if i != 0 {
  30. result += " & "
  31. }
  32. result += token
  33. if startQuotes {
  34. inQuotes = true
  35. }
  36. if clearQuotes {
  37. inQuotes = false
  38. result += ")"
  39. }
  40. }
  41. if inQuotes {
  42. return result
  43. }
  44. return strings.TrimLeft(result, " ")
  45. }