Class: ActiveRecord::ConnectionAdapters::BQAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/bq_adapter.rb

Defined Under Namespace

Classes: BindSubstitution

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, config) ⇒ BQAdapter

Returns a new instance of BQAdapter.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 26

def initialize(logger, config)
  super(nil, logger)

  @token           = nil
  @project_id      = config[:project_id]
  @client_id       = config[:client_id]
  @client_secret   = config[:client_secret]
  @refresh         = config[:refresh_token]
  @visitor         = BindSubstitution.new(self)
  @api_client_obj  = OAuth2::Client.new(@client_id, @client_secret, {:site => 'https://www.googleapis.com'})
  establish_api_connection
end

Instance Attribute Details

#project_idObject (readonly)

Returns the value of attribute project_id.



19
20
21
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 19

def project_id
  @project_id
end

Instance Method Details

#adapter_nameObject



39
40
41
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 39

def adapter_name
  "BQ"
end

#columns(table, name = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 84

def columns(table, name = nil)
  do_api do
    puts "Calling columns for #{table}"
    dataset, tablename = table.split(".")
    response = api_connection.get("#{base_url}/datasets/#{dataset}/tables/#{tablename}")
    JSON(response.body)['schema']['fields'].map do |field|
      BQColumn.new(field["name"], nil, field["type"])
    end
  end
end

#exec_insert(sql, name, binds) ⇒ Object

Raises:

  • (ActiveRecordError)


68
69
70
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 68

def exec_insert(sql, name, binds)
  raise ActiveRecordError, "Cannot use create for BQ - call ActiveRecord::Base.bq_append instead"
end

#exec_query(sql, name = 'SQL', binds = []) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 58

def exec_query(sql, name = 'SQL', binds = [])
  result = execute(sql)
  ActiveRecord::Result.new(
    result['schema']['fields'].map { |f| f['name'] },
    result['rows'].map do |row|
      row['f'].map { |col| col['v'] }
    end
  )
end

#exec_update(sql, name, binds) ⇒ Object

Raises:

  • (ActiveRecordError)


72
73
74
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 72

def exec_update(sql, name, binds)
  raise ActiveRecordError, "Update is not implemented for Google BigQuery"
end

#execute(sql, kind = "bigquery#queryRequest") ⇒ Object

This is really only suitable for queries



44
45
46
47
48
49
50
51
52
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 44

def execute(sql, kind = "bigquery#queryRequest")
  log(sql, kind) do
    do_api do
      payload = { "kind" => kind, "query"  => "#{sql};" }
      response = api_connection.post("#{base_url}/queries", :body => JSON(payload), :headers => { "Content-Type" => "application/json" })
      JSON(response.body)
    end
  end
end

#explain(sql, bind) ⇒ Object



76
77
78
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 76

def explain(sql, bind)
  " -> {Explain not implemented}"
end

#leaseObject



80
81
82
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 80

def lease
  #TODO: NOt sure why this is needed
end

#primary_key(table) ⇒ Object



104
105
106
107
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 104

def primary_key(table)
  # TODO: Doesn't really apply for BQ
  "id"
end

#schema_hash(table) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 109

def schema_hash(table)
  {
    :schema => {
      :fields => columns(table).map do |column|
        { :name => column.name, :type => column.sql_type }
      end
    }
  }
end

#select(sql, name, binds) ⇒ Object



54
55
56
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 54

def select(sql, name, binds)
  exec_query(sql, name, binds).to_a
end

#table_exists?(table) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
# File 'lib/active_record/connection_adapters/bq_adapter.rb', line 95

def table_exists?(table)
  do_api do
    puts "Calling table exists with #{table}"
    dataset, tablename = table.split(".")
    response = api_connection.get("#{base_url}/datasets/#{dataset}/tables/#{tablename}")
    response.status == 200
  end
end