Class: Rack::Insight::Database::DataTable

Inherits:
Table
  • Object
show all
Defined in:
lib/rack/insight/database.rb

Defined Under Namespace

Modules: ErrorWrapper Classes: DecodingError, EncodingError

Instance Method Summary collapse

Methods inherited from Table

#count, #create_sql, #db, #execute, #fields_sql, #insert, #keys, #length, #select

Methods included from Logging

logger, verbose, verbosity

Constructor Details

#initialize(name, *keys) ⇒ DataTable

Returns a new instance of DataTable.



185
186
187
# File 'lib/rack/insight/database.rb', line 185

def initialize(name, *keys)
  super(name, *(%w{request_id} + keys + %w{value}))
end

Instance Method Details

#count_entries(key_sql) ⇒ Object



242
243
244
# File 'lib/rack/insight/database.rb', line 242

def count_entries(key_sql)
  count(key_sql).first.first
end

#count_for_request(id) ⇒ Object



250
251
252
# File 'lib/rack/insight/database.rb', line 250

def count_for_request(id)
  count_entries("request_id = #{id}")
end

#create_keys_clauseObject



189
190
191
192
193
194
# File 'lib/rack/insight/database.rb', line 189

def create_keys_clause
  non_request_keys = @keys - %w"request_id"
  sql = non_request_keys.map{|key| "#{key} varchar"}.join(", ")
  sql += ", request_id references requests(id) on delete cascade"
  sql
end

#decode_value(value) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/rack/insight/database.rb', line 230

def decode_value(value)
  YAML.load(Base64.decode64(value))
rescue Exception => ex
  wrapped = DecodingError.new(ex, "Rack::Insight::Database#decode_value failed with error: ")
  logger.error(wrapped)
  raise wrapped if Rack::Insight::Config.database[:raise_decoding_errors]
end

#encode_value(value) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/rack/insight/database.rb', line 222

def encode_value(value)
  Base64.encode64(YAML.dump(value))
rescue Exception => ex
  wrapped = EncodingError.new(ex, "Rack::Insight::Database#encode_value failed with error: ")
  logger.error(wrapped)
  raise wrapped if Rack::Insight::Config.database[:raise_encoding_errors]
end

#for_request(id) ⇒ Object



246
247
248
# File 'lib/rack/insight/database.rb', line 246

def for_request(id)
  retrieve("request_id = #{id}")
end

#retrieve(key_sql) ⇒ Object



238
239
240
# File 'lib/rack/insight/database.rb', line 238

def retrieve(key_sql)
  select("value", key_sql).map{|value| decode_value(value.first)}
end

#store(request_id, value, keys_sql = "") ⇒ Object



196
197
198
199
200
201
# File 'lib/rack/insight/database.rb', line 196

def store(request_id, value, keys_sql = "")
  sql = "'#{encode_value(value)}'"
  sql = keys_sql + ", " + sql unless keys_sql.empty?
  sql = "#{request_id}, #{sql}"
  insert(sql)
end

#to_aObject



254
255
256
257
258
# File 'lib/rack/insight/database.rb', line 254

def to_a
  super.map do |row|
    row[-1] = decode_value(row[-1])
  end
end