Sha256: b09d81c381718ea1cd0c2b91308d0abff7b313985a870c178ef702ea1c687f27

Contents?: true

Size: 1.45 KB

Versions: 20

Compression:

Stored size: 1.45 KB

Contents

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

        begin
          response = client.transport.perform_request("POST", "_plugins/_sql", {}, {query: "#{statement} /*#{comment}*/"}).body
          columns = response["schema"].map { |v| v["name"] }
          # TODO typecast more types
          # https://github.com/opensearch-project/sql/blob/main/docs/user/general/datatypes.rst
          date_indexes = response["schema"].each_index.select { |i| response["schema"][i]["type"] == "timestamp" }
          if columns.any?
            rows = response["datarows"]
            utc = ActiveSupport::TimeZone["Etc/UTC"]
            date_indexes.each do |i|
              rows.each do |row|
                row[i] &&= utc.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

      def quoting
        # unknown
      end

      protected

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

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
blazer-3.1.0 lib/blazer/adapters/opensearch_adapter.rb
blazer-3.0.4 lib/blazer/adapters/opensearch_adapter.rb
blazer_xlsx-3.0.8 lib/blazer/adapters/opensearch_adapter.rb
blazer_xlsx-3.0.7 lib/blazer/adapters/opensearch_adapter.rb
blazer_xlsx-3.0.6 lib/blazer/adapters/opensearch_adapter.rb
blazer_xlsx-3.0.5 lib/blazer/adapters/opensearch_adapter.rb
blazer-3.0.3 lib/blazer/adapters/opensearch_adapter.rb
blazer-3.0.2 lib/blazer/adapters/opensearch_adapter.rb
finery-3.0.3 lib/blazer/adapters/opensearch_adapter.rb
finery-3.0.2 lib/blazer/adapters/opensearch_adapter.rb
blazer-3.0.1 lib/blazer/adapters/opensearch_adapter.rb
finery-3.0.1 lib/blazer/adapters/opensearch_adapter.rb
finery-3.0.0 lib/blazer/adapters/opensearch_adapter.rb
blazer-3.0.0 lib/blazer/adapters/opensearch_adapter.rb
blazer-2.6.5 lib/blazer/adapters/opensearch_adapter.rb
blazer-2.6.4 lib/blazer/adapters/opensearch_adapter.rb
blazer-2.6.3 lib/blazer/adapters/opensearch_adapter.rb
blazer-2.6.2 lib/blazer/adapters/opensearch_adapter.rb
blazer-2.6.1 lib/blazer/adapters/opensearch_adapter.rb
blazer-2.6.0 lib/blazer/adapters/opensearch_adapter.rb