Class: SqlPatches

Inherits:
Object
  • Object
show all
Defined in:
lib/patches/sql_patches.rb

Class Method Summary collapse

Class Method Details

.all_patch_filesObject



51
52
53
54
55
# File 'lib/patches/sql_patches.rb', line 51

def self.all_patch_files
  env_var = ENV["RACK_MINI_PROFILER_PATCH"]
  return [] if env_var == "false"
  env_var ? env_var.split(",").map(&:strip) : sql_patches + other_patches
end

.correct_version?(required_version, klass) ⇒ Boolean

Returns:

  • (Boolean)


2
3
4
5
6
# File 'lib/patches/sql_patches.rb', line 2

def self.correct_version?(required_version, klass)
  Gem::Dependency.new('', required_version).match?('', klass::VERSION)
rescue NameError
  false
end

.elapsed_time(start_time) ⇒ Object



20
21
22
# File 'lib/patches/sql_patches.rb', line 20

def self.elapsed_time(start_time)
  ((Time.now - start_time).to_f * 1000).round(1)
end

.other_patchesObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/patches/sql_patches.rb', line 39

def self.other_patches
  patches = []
  patches << 'mongo' if defined?(Mongo::Server::Connection) && Mongo.class == Module
  patches << 'moped' if defined?(Moped::Node) && Moped::Node.class == Class
  patches << 'plucky' if defined?(Plucky::Query) && Plucky::Query.class == Class
  patches << 'rsolr' if defined?(RSolr::Connection) && RSolr::Connection.class == Class && RSolr::VERSION[0] != "0"
  patches << 'nobrainer' if defined?(NoBrainer) && NoBrainer.class == Module
  patches << 'riak' if defined?(Riak) && Riak.class == Module
  patches << 'neo4j' if defined?(Neo4j::Core) && Neo4j::Core::Query.class == Class
  patches
end

.patch(patch_files = all_patch_files) ⇒ Object



57
58
59
60
61
# File 'lib/patches/sql_patches.rb', line 57

def self.patch(patch_files = all_patch_files)
  patch_files.each do |patch_file|
    require "patches/db/#{patch_file}"
  end
end

.record_sql(statement, parameters = nil, &block) ⇒ Object



8
9
10
11
12
13
# File 'lib/patches/sql_patches.rb', line 8

def self.record_sql(statement, parameters = nil, &block)
  start  = Time.now
  result = yield
  record = ::Rack::MiniProfiler.record_sql(statement, elapsed_time(start), parameters)
  return result, record
end

.should_measure?Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/patches/sql_patches.rb', line 15

def self.should_measure?
  current = ::Rack::MiniProfiler.current
  (current && current.measure)
end

.sql_patchesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/patches/sql_patches.rb', line 24

def self.sql_patches
  patches = []

  patches << 'mysql2' if defined?(Mysql2::Client) && Mysql2::Client.class == Class
  patches << 'pg' if defined?(PG::Result) && PG::Result.class == Class
  patches << 'oracle_enhanced' if defined?(ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter) && ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class == Class &&
                                  SqlPatches.correct_version?('~> 1.5.0', ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter)
  # if the adapters were directly patched, don't patch again
  return patches unless patches.empty?
  patches << 'sequel' if defined?(Sequel::Database) && Sequel::Database.class == Class
  patches << 'activerecord' if defined?(ActiveRecord) && ActiveRecord.class == Module

  patches
end