Class: PaperTrailViewer::DataSource::Bigquery

Inherits:
Base
  • Object
show all
Defined in:
lib/paper_trail_viewer/data_source/bigquery.rb

Defined Under Namespace

Classes: Adapter

Instance Method Summary collapse

Methods inherited from Base

#call

Constructor Details

#initialize(project_id:, credentials:, table:) ⇒ Bigquery

Returns a new instance of Bigquery.



3
4
5
6
7
8
9
10
11
# File 'lib/paper_trail_viewer/data_source/bigquery.rb', line 3

def initialize(project_id:, credentials:, table:)
  require 'google/cloud/bigquery'

  @bigquery = Google::Cloud::Bigquery.new(
    project_id:  project_id,
    credentials: credentials,
  )
  @table = table
end

Instance Method Details

#perform_query(q) ⇒ Object

Parameters:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/paper_trail_viewer/data_source/bigquery.rb', line 14

def perform_query(q)
  # https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax
  bigquery_result = @bigquery.query("    SELECT   *\n    FROM     `\#{@table}`\n    # Ignore blank versions that only touch updated_at or untracked fields.\n    WHERE    object_changes != ''\n    \#{\"AND   item_type = '\#{q.item_type}'\"        if q.item_type.present?}\n    \#{\"AND   item_id = \#{q.item_id}\"              if q.item_id.present?}\n    \#{\"AND   event = '\#{q.event}'\"                if q.event.present?}\n    \#{\"AND   object_changes LIKE '%\#{q.filter}%'\" if q.filter.present?}\n    ORDER BY created_at DESC, id DESC\n    # Paginate via OFFSET.\n    # LIMIT must be greater than `max:` or result#next? is always false.\n    LIMIT    \#{q.per_page + 1} OFFSET \#{(q.page - 1) * q.per_page}\n  SQL\n\n  Adapter.new(bigquery_result)\nend\n", max: q.per_page)