activerecord-simple_explain 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ /vendor
19
+ *~
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-simple_explain.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 wtnabe
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # ActiveRecord::SimpleExplain
2
+
3
+ ## Implemented
4
+
5
+ `ActiveRecord::Relation.explain()` method for ActiveRecord 3.0.x and 3.1.x and 3.2.x with SQLite
6
+
7
+ ## Not Implemented
8
+
9
+ auto explain
10
+
11
+ ## Supported Versions
12
+
13
+ * Ruby 1.8.7+
14
+ * ActiveRecord 3.0.x - 3.2.x
15
+
16
+ ## Supported Adapters
17
+
18
+ * SQLite
19
+ * MySQL
20
+ * PostgreSQL
21
+
22
+ ## Depending
23
+
24
+ * [cldwalker/hirb](https://github.com/cldwalker/hirb)
25
+ * [miaout17/hirb-unicode](https://github.com/miaout17/hirb-unicode)
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ gem 'activerecord-simple_explain'
32
+
33
+ And then execute:
34
+
35
+ $ bundle
36
+
37
+ Or install it yourself as:
38
+
39
+ $ gem install activerecord-simple_explain
40
+
41
+ ## Usage
42
+
43
+ just call ActiveRecord::Relation.explain()
44
+
45
+ ex)
46
+
47
+ Blog.joins(:comments).explain
48
+
49
+ on Rails console ( Rails 3.2 + SQLite Adapter )
50
+
51
+ development(main):001:0> Blog.joins(:comments).explain
52
+ (0.1ms) explain query plan SELECT "blogs".* FROM "blogs" INNER JOIN "comments" ON "comments"."blog_id" = "blogs"."id"
53
+ +------------------------------------------------------------------+------+-------+----------+
54
+ | detail | from | order | selectid |
55
+ +------------------------------------------------------------------+------+-------+----------+
56
+ | SCAN TABLE comments (~1000000 rows) | 1 | 0 | 0 |
57
+ | SEARCH TABLE blogs USING INTEGER PRIMARY KEY (rowid=?) (~1 rows) | 0 | 1 | 0 |
58
+ +------------------------------------------------------------------+------+-------+----------+
59
+ 2 rows in set
60
+ => nil
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # -*- mode: ruby; coding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/simple_explain/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-simple_explain"
8
+ spec.version = ActiveRecord::SimpleExplain::VERSION
9
+ spec.authors = ["wtnabe"]
10
+ spec.email = ["wtnabe@gmail.com"]
11
+ spec.description = %q{explain method for ActiveRecord 3.0.x and 3.1.x}
12
+ spec.summary = ""
13
+ spec.homepage = "https://github.com/wtnabe/activerecord-simple_explain"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = ">= 1.8.7"
22
+
23
+ spec.add_runtime_dependency "activerecord", "< 4.0", ">= 3.0"
24
+ spec.add_runtime_dependency "hirb-unicode"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rake"
28
+ end
@@ -0,0 +1,61 @@
1
+ require 'hirb-unicode'
2
+
3
+ module ActiveRecord
4
+ class SimpleExplain
5
+ #
6
+ # [param] String adapter
7
+ #
8
+ def initialize(adapter, sql)
9
+ #
10
+ # {class => filename}
11
+ #
12
+ @adapters = {}
13
+ init_adapters
14
+
15
+ @sql = sql
16
+ load_adapter(adapter)
17
+ end
18
+ attr_reader :sql
19
+
20
+ def init_adapters
21
+ %w(SQLite3Adapter sqlite3_adapter
22
+ PostgreSQLAdapter postgresql_adapter).each_cons(2) { |k, v|
23
+ add_adapter(k, v)
24
+ }
25
+ end
26
+
27
+ def add_adapter(klass, filename)
28
+ @adapters[klass] = filename
29
+ end
30
+
31
+ def load_adapter(adapter)
32
+ require File.expand_path(File.dirname(__FILE__) + "/simple_explain/connection_adapters/#{filename(adapter)}")
33
+ self.class_eval {
34
+ include ActiveRecord::SimpleExplain.const_get(adapter)
35
+ }
36
+ end
37
+
38
+ def filename(adapter)
39
+ if @adapters.has_key? adapter
40
+ @adapters[adapter]
41
+ else
42
+ adapter.underscore
43
+ end
44
+ end
45
+
46
+ def display
47
+ puts format(result)
48
+ end
49
+
50
+ #
51
+ # [return] Object ( Adapter::Result or Array )
52
+ #
53
+ def result
54
+ ActiveRecord::Base.connection.execute("#{statement} #{sql}")
55
+ end
56
+ end
57
+ end
58
+
59
+ Dir.glob(File.dirname(__FILE__) + '/simple_explain/*.rb').each {|f|
60
+ require f.sub(/\.rb/, '')
61
+ }
@@ -0,0 +1,15 @@
1
+ module ActiveRecord
2
+ class SimpleExplain
3
+ module AdapterInterface
4
+ class NotImplememted < StandardError; end
5
+
6
+ def format(result)
7
+ raise NotImplememted
8
+ end
9
+
10
+ def statement
11
+ raise NotImplememted
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/adapter_interface')
2
+
3
+ module ActiveRecord
4
+ class SimpleExplain
5
+ module MysqlAdapter
6
+ include AdapterInterface
7
+
8
+ #
9
+ # [param] Object
10
+ # [return] String
11
+ #
12
+ def format(result)
13
+ Hirb::Helpers::AutoTable.render(pre_format(result))
14
+ end
15
+
16
+ #
17
+ # [param] Object
18
+ # [return] Array
19
+ #
20
+ def pre_format(result)
21
+ result.to_a.map {|r|
22
+ Hash[*result.fields.zip(r).flatten]
23
+ }
24
+ end
25
+
26
+ #
27
+ # [return] String
28
+ #
29
+ def statement
30
+ 'explain'
31
+ end
32
+ end
33
+ end
34
+ end
35
+
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/adapter_interface')
2
+
3
+ module ActiveRecord
4
+ class SimpleExplain
5
+ module PostgreSQLAdapter
6
+ include AdapterInterface
7
+
8
+ #
9
+ # [param] Object
10
+ # [return] String
11
+ #
12
+ def format(result)
13
+ result.values.join("\n")
14
+ end
15
+
16
+ #
17
+ # [return] String
18
+ #
19
+ def statement
20
+ 'explain'
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/adapter_interface')
2
+
3
+ module ActiveRecord
4
+ class SimpleExplain
5
+ module SQLite3Adapter
6
+ include AdapterInterface
7
+
8
+ #
9
+ # [param] Array
10
+ # [return] String
11
+ #
12
+ def format(result)
13
+ Hirb::Helpers::AutoTable.render(pre_format(result))
14
+ end
15
+
16
+ #
17
+ # [param] Array
18
+ # [return] Hash
19
+ #
20
+ def pre_format(result)
21
+ result.map {|e|
22
+ Hash[*e.select {|k, v|
23
+ k.is_a? String
24
+ }.flatten]
25
+ }
26
+ end
27
+
28
+ #
29
+ # [return] String
30
+ #
31
+ def statement
32
+ 'explain query plan'
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,25 @@
1
+ module ActiveRecord
2
+ eager_autoload do
3
+ autoload :Relation
4
+ end
5
+
6
+ class Relation
7
+ alias_method :explain_org, :explain if method_defined? :explain
8
+
9
+ def explain
10
+ adapter = ActiveRecord::Base.connection.class.to_s.split(/::/).last
11
+
12
+ exp = proc { SimpleExplain.new(adapter, to_sql).display }
13
+
14
+ if method_defined? :explain_org
15
+ if adapter == 'sqlite3'
16
+ exp.call
17
+ else
18
+ explain_org
19
+ end
20
+ else
21
+ exp.call
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ class SimpleExplain
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require "active_record/simple_explain"
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-simple_explain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - wtnabe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - <
20
+ - !ruby/object:Gem::Version
21
+ version: '4.0'
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '3.0'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: '4.0'
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '3.0'
36
+ - !ruby/object:Gem::Dependency
37
+ name: hirb-unicode
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ - !ruby/object:Gem::Dependency
53
+ name: bundler
54
+ requirement: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: '1.3'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: '1.3'
68
+ - !ruby/object:Gem::Dependency
69
+ name: rake
70
+ requirement: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: explain method for ActiveRecord 3.0.x and 3.1.x
85
+ email:
86
+ - wtnabe@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - activerecord-simple_explain.gemspec
97
+ - lib/active_record/simple_explain.rb
98
+ - lib/active_record/simple_explain/connection_adapters/adapter_interface.rb
99
+ - lib/active_record/simple_explain/connection_adapters/mysql_adapter.rb
100
+ - lib/active_record/simple_explain/connection_adapters/postgresql_adapter.rb
101
+ - lib/active_record/simple_explain/connection_adapters/sqlite3_adapter.rb
102
+ - lib/active_record/simple_explain/relation.rb
103
+ - lib/active_record/simple_explain/version.rb
104
+ - lib/activerecord-simple_explain.rb
105
+ homepage: https://github.com/wtnabe/activerecord-simple_explain
106
+ licenses:
107
+ - MIT
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 1.8.7
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ segments:
125
+ - 0
126
+ hash: 1464302755730536436
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 1.8.23
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: ''
133
+ test_files: []