Class: Sequel::Bigquery::Database

Inherits:
Database
  • Object
show all
Defined in:
lib/sequel-bigquery.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Constructor Details

#initialize(*args, **kwargs) ⇒ Database

Returns a new instance of Database.



20
21
22
23
24
25
# File 'lib/sequel-bigquery.rb', line 20

def initialize(*args, **kwargs)
  @bigquery_config = kwargs.fetch(:orig_opts)
  @sql_buffer = []
  @sql_buffering = false
  super
end

Instance Method Details

#bigqueryObject



33
34
35
36
# File 'lib/sequel-bigquery.rb', line 33

def bigquery
  # ObjectSpace.each_object(HTTPClient).each { |c| c.debug_dev = STDOUT }
  @bigquery ||= Google::Cloud::Bigquery.new(google_cloud_bigquery_gem_config)
end

#connect(*_args) ⇒ Object



27
28
29
30
31
# File 'lib/sequel-bigquery.rb', line 27

def connect(*_args)
  log_each(:debug, '#connect')
  get_or_create_bigquery_dataset
    .tap { log_each(:debug, '#connect end') }
end

#disconnect_connection(_c) ⇒ Object



38
39
40
41
# File 'lib/sequel-bigquery.rb', line 38

def disconnect_connection(_c)
  log_each(:debug, '#disconnect_connection')
  # c.disconnect
end

#drop_datasets(*dataset_names_to_drop) ⇒ Object Also known as: drop_dataset



43
44
45
46
47
48
49
50
51
# File 'lib/sequel-bigquery.rb', line 43

def drop_datasets(*dataset_names_to_drop)
  dataset_names_to_drop.each do |dataset_name_to_drop|
    log_each(:debug, "Dropping dataset #{dataset_name_to_drop.inspect}")
    dataset_to_drop = bigquery.dataset(dataset_name_to_drop)
    next unless dataset_to_drop
    dataset_to_drop.tables.each(&:delete)
    dataset_to_drop.delete
  end
end

#execute(sql, opts = OPTS) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sequel-bigquery.rb', line 54

def execute(sql, opts = OPTS) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  log_each(:debug, '#execute')
  log_query(sql)

  # require 'pry'; binding.pry if sql =~ /CREATE TABLE IF NOT EXISTS/i

  sql = sql.gsub(/\sdefault \S+/i) do
    warn_default_removal(sql)
    ''
  end

  if sql =~ /^update/i && sql !~ / where /i
    warn("Warning: Appended 'where 1 = 1' to query since BigQuery requires UPDATE statements to include a WHERE clause")
    sql += ' where 1 = 1'
  end

  if /^begin/i.match?(sql)
    warn_transaction
    @sql_buffering = true
  end

  if @sql_buffering
    @sql_buffer << sql
    return [] unless /^commit/i.match?(sql)
    warn("Warning: Will now execute entire buffered transaction:\n" + @sql_buffer.join("\n"))
  end

  sql_to_execute = @sql_buffer.any? ? @sql_buffer.join("\n") : sql

  synchronize(opts[:server]) do |conn|
    results = log_connection_yield(sql, conn) do
      conn.query(sql_to_execute)
    end
    log_each(:debug, results.awesome_inspect)
    if block_given?
      yield results
    else
      results
    end
  rescue Google::Cloud::InvalidArgumentError, Google::Cloud::PermissionDeniedError => e
    if e.message.include?('too many table update operations for this table')
      warn('Triggered rate limit of table update operations for this table. For more information, see https://cloud.google.com/bigquery/docs/troubleshoot-quotas')
      if retryable_query?(sql_to_execute)
        warn('Detected retryable query - re-running query after a 1 second sleep')
        sleep 1
        retry
      else
        log_each(:error, "Query not detected as retryable; can't automatically recover from being rate-limited")
      end
    end
    raise_error(e)
  rescue ArgumentError => e
    raise_error(e)
  end # rubocop:disable Style/MultilineBlockChain
    .tap do
      @sql_buffer = []
      @sql_buffering = false
    end
end

#supports_create_table_if_not_exists?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/sequel-bigquery.rb', line 114

def supports_create_table_if_not_exists?
  true
end

#type_literal_generic_float(_column) ⇒ Object



126
127
128
# File 'lib/sequel-bigquery.rb', line 126

def type_literal_generic_float(_column)
  :float64
end

#type_literal_generic_string(column) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/sequel-bigquery.rb', line 118

def type_literal_generic_string(column)
  if column[:size]
    "string(#{column[:size]})"
  else
    :string
  end
end