Class: SqlPatches

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

Class Method Summary collapse

Class Method Details

.all_patch_filesObject



61
62
63
64
65
# File 'lib/patches/sql_patches.rb', line 61

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)


4
5
6
7
8
# File 'lib/patches/sql_patches.rb', line 4

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

.elapsed_time(start_time) ⇒ Object



22
23
24
# File 'lib/patches/sql_patches.rb', line 22

def self.elapsed_time(start_time)
  ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time).to_f * 1000).round(1)
end

.other_patchesObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/patches/sql_patches.rb', line 49

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



67
68
69
70
71
# File 'lib/patches/sql_patches.rb', line 67

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

.patch_rails?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/patches/sql_patches.rb', line 26

def self.patch_rails?
  ::Rack::MiniProfiler.patch_rails?
end

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



10
11
12
13
14
15
# File 'lib/patches/sql_patches.rb', line 10

def self.record_sql(statement, parameters = nil, &block)
  start  = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  result = yield
  record = ::Rack::MiniProfiler.record_sql(statement, elapsed_time(start), parameters)
  [result, record]
end

.should_measure?Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/patches/sql_patches.rb', line 17

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

.sql_patchesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/patches/sql_patches.rb', line 30

def self.sql_patches
  patches = []

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