Class: BigQueryAdapter::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/big_query_adapter/connection.rb

Overview

Driver for bigquery connection

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project:, keyfile:, timeout: nil, datasets: []) ⇒ Connection

Returns a new instance of Connection.



11
12
13
14
15
16
17
18
19
# File 'lib/big_query_adapter/connection.rb', line 11

def initialize(project:, keyfile:, timeout: nil, datasets: [])
  @project = project
  @bigquery = Google::Cloud::Bigquery.new(
    project: project,
    keyfile: keyfile
  )
  @dataset_ids = datasets
  @timeout = timeout.to_i if timeout
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



9
10
11
# File 'lib/big_query_adapter/connection.rb', line 9

def project
  @project
end

Instance Method Details

#columns(table_name) ⇒ Object



43
44
45
46
47
# File 'lib/big_query_adapter/connection.rb', line 43

def columns(table_name)
  table_schema = table_schema(table_name)
  return [] if table_schema.fields.nil?
  table_schema.fields
end

#run(statement) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/big_query_adapter/connection.rb', line 21

def run(statement)
  columns = []
  rows = []

  options = {}
  options[:timeout] = @timeout if @timeout
  results = @bigquery.query(statement, options) # ms
  if results.complete?
    columns = results.first.keys.map(&:to_s) unless results.empty?
    rows = results.map(&:values)
  end

  Result.new(columns, rows)
end

#tablesObject



36
37
38
39
40
41
# File 'lib/big_query_adapter/connection.rb', line 36

def tables
  table_refs
    .map { |table_ref| table_ref_name(table_ref) }
    .group_by { |table_ref_name| table_ref_wildcard_name(table_ref_name) }
    .keys
end