pgexplain 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Ramon Salvadó
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,62 @@
1
+ = pgexplain
2
+
3
+ Authors:: Ramon Salvadó <rsalvado@gnuine.com>
4
+
5
+ * RubyForge_Project_Page[http://rubyforge.org/projects/pgexplain/]
6
+ * Github Project [http://github.com/marcus-wyatt/pgexplain]
7
+
8
+ Any questions/comments can be addressed to Ramon Salvadó <rsalvado@gnuine.com>
9
+
10
+ This Rails plugin shows the output, in your application logs, of applying "Explain" or "Explain Analyze" to the select queries of your rails application.
11
+ It should be used with a PostgreSQL database.
12
+
13
+ == Dependencies
14
+
15
+ Needs to have the pg gem installed:
16
+
17
+ <code>sudo gem install pg</code>
18
+
19
+ == Installation
20
+
21
+ To install as a gem:
22
+
23
+ <code>sudo gem install pgexplain</code>
24
+
25
+ To install the plugin:
26
+
27
+ Github:
28
+ <code>script/plugin install svn://rubyforge.org/var/svn/pgexplain</code>
29
+
30
+ Original:
31
+ <code>script/plugin install svn://rubyforge.org/var/svn/pgexplain</code>
32
+
33
+ == Features
34
+
35
+ - By default works only in development environment and doing just explains.
36
+ - Colors can be turned on off (this is useful if you want to look the logs later from the log file).
37
+ - Instead of doing explains it can be configured to do explain analyze.
38
+ - It can be disabled and enabled (this is useful if you want to turn it off in the console, for example).
39
+
40
+ == Configuration
41
+
42
+ - In you environment.rb
43
+
44
+ PgExplain::configure :color => false, :analyze = true
45
+
46
+ - To disable the plugin
47
+
48
+ PgExplain::disable
49
+
50
+ - To enable the plugin
51
+
52
+ PgExplain::enable
53
+
54
+ == Needed things
55
+
56
+ * PostgreSQL database
57
+ * postgres gem: sudo gem install postgres
58
+
59
+ == Acknowledgments:
60
+
61
+ - Bob Silva for his Query Analyzer plugin from which I borrowed some code/concepts.
62
+ - Marcus Wyatt for packaging the into a gem.
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "pgexplain"
8
+ gem.summary = %Q{Outputs "Explain" or "Explain Analyze" in your developement application log.}
9
+ gem.description = %Q{The Rails gem shows the output, in your application logs, of applying "Explain" or "Explain Analyze" to the select queries of your rails application.}
10
+ gem.email = "marcus.wyatt@visfleet.com, rsalvado@gnuine.com"
11
+ gem.homepage = "http://github.com/marcus-wyatt/pgexplain"
12
+ gem.authors = ["Ramon Salvadó", "Marcus Wyatt"]
13
+ # gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem.add_development_dependency "cucumber", ">= 0"
15
+ gem.add_dependency 'pg', ">= 0.8.0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ desc 'Default: run unit tests.'
24
+ task :default => :test
25
+
26
+ begin
27
+ require 'yard'
28
+ YARD::Rake::YardocTask.new
29
+ rescue LoadError
30
+ task :yardoc do
31
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
32
+ end
33
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'pg_explain'
2
+ require 'adapter_extensions'
3
+
4
+ PgExplain::configure :color => true, :analyze => false
5
+ PgExplain::enable if Rails.evn == "development" # Get Ready for Rails 3.0 (RAILS_ENV deprecated.)
@@ -0,0 +1,21 @@
1
+ module ActiveRecord #:nodoc:
2
+ module ConnectionAdapters #:nodoc:
3
+ class PostgreSQLAdapter < AbstractAdapter #:nodoc:
4
+ private
5
+ alias_method :select_without_analyzer, :select
6
+
7
+ def select(sql, name = nil)
8
+ query_results = select_without_analyzer(sql, name)
9
+ if @logger and Pg_explain::enabled? and @logger.level <= Logger::DEBUG
10
+ @logger.debug(
11
+ @logger.silence do
12
+ message = Pg_explain::format(select_without_analyzer("#{Pg_explain::method} #{sql}", name))
13
+ format_log_entry("#{Pg_explain::method} #{name}\n", message)
14
+ end
15
+ ) if sql =~ /^select/i
16
+ end
17
+ query_results
18
+ end
19
+ end
20
+ end
21
+ end
data/lib/pg_explain.rb ADDED
@@ -0,0 +1,51 @@
1
+ # This module contains:
2
+ # - Activation/disactivation methods
3
+ # - Configuration methods
4
+ # - Output formatting methods
5
+ module PgExplain
6
+ mattr_accessor :color
7
+ mattr_accessor :analyze
8
+
9
+ @@color = false
10
+ @@analyze = false
11
+ @@enable = false
12
+
13
+ # Activates the plugin
14
+ def self.enable
15
+ @@enable = true
16
+ end
17
+
18
+ # Disables the plugin
19
+ def self.disable
20
+ @@enable = false
21
+ end
22
+
23
+ # Check if the plugin is enabled
24
+ def self.enabled?
25
+ @@enable
26
+ end
27
+
28
+ # To setup the configuration options
29
+ def self.configure(options = {})
30
+ @@color ||= options[:color]
31
+ @@analyze ||= options[:analyze]
32
+ end
33
+
34
+ # Returns "Explain" or "Explain Analyze"
35
+ def self.method
36
+ ("Explain Analyze" if analyze) || "Explain"
37
+ end
38
+
39
+ # Formats the results of the explain query
40
+ def self.format(message)
41
+ formatted_message = "\n" + message.collect { |line| " " + colorize(line["QUERY PLAN"]) }.join("\n") + "\n"
42
+ color ? "\033[01;30m#{formatted_message}\033[00m" : formatted_message
43
+ end
44
+
45
+ private
46
+
47
+ # Colorizes sequential scans
48
+ def self.colorize(line)
49
+ color ? line.gsub("Seq Scan", "\033[01;31mSeq Scan\033[01;30m") : line
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pgexplain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "Ramon Salvad\xC3\xB3"
8
+ - Marcus Wyatt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-02-08 00:00:00 +13:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: pg
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.8.0
25
+ version:
26
+ description: The Rails gem shows the output, in your application logs, of applying "Explain" or "Explain Analyze" to the select queries of your rails application.
27
+ email: marcus.wyatt@visfleet.com, rsalvado@gnuine.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - LICENSE
34
+ - README.rdoc
35
+ files:
36
+ - .document
37
+ - .gitignore
38
+ - LICENSE
39
+ - README.rdoc
40
+ - Rakefile
41
+ - VERSION
42
+ - init.rb
43
+ - lib/adapter_extensions.rb
44
+ - lib/pg_explain.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/marcus-wyatt/pgexplain
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Outputs "Explain" or "Explain Analyze" in your developement application log.
73
+ test_files: []
74
+