g00k-explain-query 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +11 -0
- data/README.rdoc +65 -0
- data/Rakefile +16 -0
- data/lib/explain-query.rb +13 -0
- data/lib/explain-query/explain-query.rb +63 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/test/test_explain-query.rb +11 -0
- data/test/test_helper.rb +3 -0
- metadata +82 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
== DESCRIPTION:
|
2
|
+
|
3
|
+
The explain-query gem allows you to 'EXPLAIN' your ActiveRecord::Base#find calls from the safety of your Rails console.
|
4
|
+
|
5
|
+
== FEATURES/PROBLEMS:
|
6
|
+
|
7
|
+
The library adds the #explain method to ActiveRecord::Base (ie. all your models.)
|
8
|
+
|
9
|
+
== SYNOPSIS:
|
10
|
+
|
11
|
+
Here's a quick sample:
|
12
|
+
|
13
|
+
>> Post.explain do
|
14
|
+
?> Post.first
|
15
|
+
>> end
|
16
|
+
SELECT * FROM `posts` LIMIT 1
|
17
|
+
select_type | key_len | table | id | possible_keys | type | Extra | rows | ref | key
|
18
|
+
------------------------------------------------------------------------------------
|
19
|
+
SIMPLE | | posts | 1 | | ALL | | 6 | |
|
20
|
+
|
21
|
+
=> #<Post id: 2, title: "First Post", content: "some content\r\n", created_at: "2009-07-07 09:19:46", updated_at: "2009-07-17 14:15:01", permalink: "first-post">
|
22
|
+
|
23
|
+
== REQUIREMENTS:
|
24
|
+
|
25
|
+
This library obviously requires the ActiveRecord library.
|
26
|
+
|
27
|
+
== INSTALL:
|
28
|
+
|
29
|
+
To install, simply run:
|
30
|
+
|
31
|
+
sudo gem install explain-query
|
32
|
+
|
33
|
+
and then require it from your Rails console:
|
34
|
+
|
35
|
+
$ ./script/console
|
36
|
+
>> require 'explain-query'
|
37
|
+
|
38
|
+
== LICENSE:
|
39
|
+
|
40
|
+
(The MIT License)
|
41
|
+
|
42
|
+
Copyright (c) 2009 FIXME full name
|
43
|
+
|
44
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
45
|
+
a copy of this software and associated documentation files (the
|
46
|
+
'Software'), to deal in the Software without restriction, including
|
47
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
48
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
49
|
+
permit persons to whom the Software is furnished to do so, subject to
|
50
|
+
the following conditions:
|
51
|
+
|
52
|
+
The above copyright notice and this permission notice shall be
|
53
|
+
included in all copies or substantial portions of the Software.
|
54
|
+
|
55
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
56
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
57
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
58
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
59
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
60
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
61
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
62
|
+
|
63
|
+
== CREDITS:
|
64
|
+
|
65
|
+
This gem is pretty much just a gemified version of Bob Silva's (http://nfectio.us/) query-analyzer Rails plugin, with the addition of the block syntax instead of writing output to the logfile. Thanks!
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
require 'hoe'
|
4
|
+
require 'fileutils'
|
5
|
+
require './lib/explain-query'
|
6
|
+
|
7
|
+
Hoe.plugin :newgem
|
8
|
+
|
9
|
+
# Generate all the Rake tasks
|
10
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
11
|
+
$hoe = Hoe.spec 'explain-query' do
|
12
|
+
self.developer 'Gustav Paul', 'gustav@rails.co.za'
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'newgem/tasks'
|
16
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
@@ -0,0 +1,13 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
|
3
|
+
module ExplainQuery
|
4
|
+
VERSION = '0.0.1'
|
5
|
+
end
|
6
|
+
|
7
|
+
if defined?(ActiveRecord::ConnectionAdapters::MysqlAdapter)
|
8
|
+
require 'explain-query/explain-query'
|
9
|
+
|
10
|
+
Array.send :include, ExplainQuery::ArrayExt
|
11
|
+
ActiveRecord::ConnectionAdapters::MysqlAdapter.send :include, ExplainQuery::MysqlAdapterExt
|
12
|
+
ActiveRecord::Base.send :extend, ExplainQuery::Watcher
|
13
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module ExplainQuery
|
2
|
+
module ArrayExt
|
3
|
+
protected
|
4
|
+
def qa_columnized_row(fields, sized)
|
5
|
+
row = []
|
6
|
+
fields.each_with_index do |f, i|
|
7
|
+
row << sprintf("%0-#{sized[i]}s", f.to_s)
|
8
|
+
end
|
9
|
+
row.join(' | ')
|
10
|
+
end
|
11
|
+
|
12
|
+
public
|
13
|
+
|
14
|
+
def qa_columnized
|
15
|
+
sized = {}
|
16
|
+
self.each do |row|
|
17
|
+
row.values.each_with_index do |value, i|
|
18
|
+
sized[i] = [sized[i].to_i, row.keys[i].length, value.to_s.length].max
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
table = []
|
23
|
+
table << qa_columnized_row(self.first.keys, sized)
|
24
|
+
table << '-' * table.first.length
|
25
|
+
self.each { |row| table << qa_columnized_row(row.values, sized) }
|
26
|
+
table.join("\n ") # Spaces added to work with format_log_entry
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module Watcher
|
31
|
+
def explain
|
32
|
+
raise(ArgumentError, "No block given") if !block_given?
|
33
|
+
begin
|
34
|
+
ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:alias_method, :select_without_explain, :select)
|
35
|
+
ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:alias_method, :select, :select_with_explain)
|
36
|
+
result = yield
|
37
|
+
ensure
|
38
|
+
ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:alias_method, :select_with_explain, :select)
|
39
|
+
ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:alias_method, :select, :select_without_explain)
|
40
|
+
end
|
41
|
+
|
42
|
+
result
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module MysqlAdapterExt
|
47
|
+
private
|
48
|
+
|
49
|
+
def select_with_explain(sql, name = nil)
|
50
|
+
query_results = select_without_explain(sql, name)
|
51
|
+
|
52
|
+
if sql =~ /^select/i
|
53
|
+
output = ActiveRecord::Base.silence do
|
54
|
+
explain_results = select_without_explain("explain #{sql}", name)
|
55
|
+
format_log_entry("\033[1;34m#{sql} \033[0m\n", "#{explain_results.qa_columnized}\n")
|
56
|
+
end
|
57
|
+
puts output
|
58
|
+
end
|
59
|
+
|
60
|
+
query_results
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/explain-query.rb'}"
|
9
|
+
puts "Loading explain-query gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: g00k-explain-query
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gustav Paul
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-18 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mime-types
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "1.15"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: diff-lcs
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.2
|
34
|
+
version:
|
35
|
+
description: ExplainQuery let's you see the output of Mysql's EXPLAIN command from your Rails console.
|
36
|
+
email: gustav@rails.co.za
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- History.txt
|
45
|
+
- README.rdoc
|
46
|
+
- Rakefile
|
47
|
+
- Manifest.txt
|
48
|
+
- lib/explain-query.rb
|
49
|
+
- lib/explain-query/explain-query.rb
|
50
|
+
- script/console
|
51
|
+
- script/destroy
|
52
|
+
- script/generate
|
53
|
+
- test/test_helper.rb
|
54
|
+
- test/test_explain-query.rb
|
55
|
+
has_rdoc: false
|
56
|
+
homepage: http://github.com/g00k/explain-query
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project: explain-query
|
77
|
+
rubygems_version: 1.2.0
|
78
|
+
signing_key:
|
79
|
+
specification_version: 2
|
80
|
+
summary: ExplainQuery let's you see the output of Mysql's EXPLAIN command from your Rails console.
|
81
|
+
test_files: []
|
82
|
+
|