Sha256: 8ad4c7a5ac289b77a7ddd0ff32c978d7e11cb34fd4359861544f0aa7f9895485

Contents?: true

Size: 1.26 KB

Versions: 43

Compression:

Stored size: 1.26 KB

Contents

module Blazer
  module Adapters
    class ElasticsearchAdapter < BaseAdapter
      def run_statement(statement, comment)
        columns = []
        rows = []
        error = nil

        begin
          response = client.xpack.sql.query(body: {query: "#{statement} /*#{comment}*/"})
          columns = response["columns"].map { |v| v["name"] }
          # Elasticsearch does not differentiate between dates and times
          date_indexes = response["columns"].each_index.select { |i| response["columns"][i]["type"] == "date" }
          if columns.any?
            rows = response["rows"]
            date_indexes.each do |i|
              rows.each do |row|
                row[i] = Time.parse(row[i])
              end
            end
          end
        rescue => e
          error = e.message
        end

        [columns, rows, error]
      end

      def tables
        indices = client.cat.indices(format: "json").map { |v| v["index"] }
        aliases = client.cat.aliases(format: "json").map { |v| v["alias"] }
        (indices + aliases).uniq.sort
      end

      def preview_statement
        "SELECT * FROM \"{table}\" LIMIT 10"
      end

      protected

      def client
        @client ||= Elasticsearch::Client.new(url: settings["url"])
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 3 rubygems

Version Path
sql-jarvis-2.1.10 lib/blazer/adapters/elasticsearch_adapter.rb
sql-jarvis-2.1.9 lib/blazer/adapters/elasticsearch_adapter.rb
sql-jarvis-2.1.8 lib/blazer/adapters/elasticsearch_adapter.rb
sql-jarvis-2.1.7 lib/blazer/adapters/elasticsearch_adapter.rb
sql-jarvis-2.1.6 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.4.7 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.4.6 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.4.5 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.4.4 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.4.3 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.4.2 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.4.1 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.4.0 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.3.1 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.3.0 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.2.8 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.2.7 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.2.6 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.2.5 lib/blazer/adapters/elasticsearch_adapter.rb
blazer-2.2.4 lib/blazer/adapters/elasticsearch_adapter.rb