presto_adapter.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. module Blazer
  2. module Adapters
  3. class PrestoAdapter < BaseAdapter
  4. def run_statement(statement, comment)
  5. columns = []
  6. rows = []
  7. error = nil
  8. begin
  9. columns, rows = client.run("#{statement} /*#{comment}*/")
  10. columns = columns.map(&:name)
  11. rescue => e
  12. error = e.message
  13. end
  14. [columns, rows, error]
  15. end
  16. def tables
  17. _, rows = client.run("SHOW TABLES")
  18. rows.map(&:first)
  19. end
  20. def preview_statement
  21. "SELECT * FROM {table} LIMIT 10"
  22. end
  23. protected
  24. def client
  25. @client ||= begin
  26. uri = URI.parse(settings["url"])
  27. query = uri.query ? CGI::parse(uri.query) : {}
  28. Presto::Client.new(
  29. server: "#{uri.host}:#{uri.port}",
  30. catalog: uri.path.to_s.sub(/\A\//, ""),
  31. schema: query["schema"] || "public",
  32. user: uri.user,
  33. http_debug: false
  34. )
  35. end
  36. end
  37. end
  38. end
  39. end