1
0

dialect.go 865 B

12345678910111213141516171819202122232425262728
  1. package database
  2. import "fmt"
  3. // JSONClientsFromInbound returns the FROM clause that yields one row per element
  4. // of inbounds.settings -> clients, with a column named `client.value` whose text
  5. // fields can be read with JSONFieldText("client.value", "<key>").
  6. func JSONClientsFromInbound() string {
  7. if IsPostgres() {
  8. return "FROM inbounds, jsonb_array_elements(inbounds.settings::jsonb -> 'clients') AS client(value)"
  9. }
  10. return "FROM inbounds, JSON_EACH(JSON_EXTRACT(inbounds.settings, '$.clients')) AS client"
  11. }
  12. func JSONFieldText(expr, key string) string {
  13. if IsPostgres() {
  14. return fmt.Sprintf("(%s ->> '%s')", expr, key)
  15. }
  16. return fmt.Sprintf("TRIM(JSON_EXTRACT(%s, '$.%s'), '\"')", expr, key)
  17. }
  18. func GreatestExpr(a, b string) string {
  19. if IsPostgres() {
  20. return fmt.Sprintf("GREATEST(%s, %s)", a, b)
  21. }
  22. return fmt.Sprintf("MAX(%s, %s)", a, b)
  23. }