Sha256: 498ff8c27e77b6c75caff92cd8283a78d184381359a69dc8b53a2d02af4c5a51

Contents?: true

Size: 1.74 KB

Versions: 46

Compression:

Stored size: 1.74 KB

Contents

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

        begin
          results = bigquery.query(statement)

          # complete? was removed in google-cloud-bigquery 0.29.0
          # code is for backward compatibility
          if !results.respond_to?(:complete?) || results.complete?
            columns = results.first.keys.map(&:to_s) if results.size > 0
            rows = results.map(&:values)
          else
            error = Blazer::TIMEOUT_MESSAGE
          end
        rescue => e
          error = e.message
        end

        [columns, rows, error]
      end

      def tables
        table_refs.map { |t| "#{t.project_id}.#{t.dataset_id}.#{t.table_id}" }
      end

      def schema
        table_refs.map do |table_ref|
          {
            schema: table_ref.dataset_id,
            table: table_ref.table_id,
            columns: table_columns(table_ref)
          }
        end
      end

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

      private

      def bigquery
        @bigquery ||= begin
          require "google/cloud/bigquery"
          Google::Cloud::Bigquery.new(
            project: settings["project"],
            keyfile: settings["keyfile"]
          )
        end
      end

      def table_refs
        bigquery.datasets.map(&:tables).flat_map { |table_list| table_list.map(&:table_ref) }
      end

      def table_columns(table_ref)
        schema = bigquery.service.get_table(table_ref.dataset_id, table_ref.table_id).schema
        return [] if schema.nil?
        schema.fields.map { |field| {name: field.name, data_type: field.type} }
      end
    end
  end
end

Version data entries

46 entries across 46 versions & 3 rubygems

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