tune_my_query 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY ADDED
File without changes
data/LICENSE ADDED
File without changes
data/README.markdown ADDED
@@ -0,0 +1,58 @@
1
+ tune_my_query
2
+ ============
3
+
4
+ About
5
+ -----
6
+ **tune_my_query** is an extension of the gem **me_gusta** written by **[John Pignata]**(http://github.com/jpignata)
7
+
8
+ It is a simple way to switch between standard SQL and extended SQL syntax depending on the adapter. For example: PostgreSql has an ILIKE extension which is not found in other databases like mysql. This gem(not released yet) ensures the query is adapted based on the current database.
9
+
10
+
11
+ Inspiration
12
+ -----------
13
+ Gemcutter uses PostgreSql in production and uses ILIKE in one of the queries. For anyone, hacking on their local machine will have to change it to LIKE for it to work. Also, when it comes to accepting patches from a MySql user this would again pose a minor issue. Hence, this gem.
14
+
15
+
16
+ Usage
17
+ -----
18
+
19
+ It is simple to use. Just install it like:
20
+
21
+ sudo gem install tune_my_query
22
+
23
+ And do in the model class where you have used a database specific SQL, like this:
24
+
25
+ tune_my_query :like
26
+
27
+ Here like signifies the extension to usual SQL LIKE.
28
+
29
+
30
+ Does it only work with LIKE? What a futile effort
31
+ -------------------------------------------------
32
+ Yes, at the moment it only does that. But, you can fork the code and add your own commands in the commands directory inside lib. Every new command that you add should have a name ending with Command. For example: AwesomeCommand. Add a singleton execute method to it and send me a patch, I will add it in. Please do add specs.
33
+
34
+
35
+ Specs
36
+ -----
37
+ In order to run the specs you should have mysql and postgresql installed. [Here's](http://developer.apple.com/internet/opensource/postgres.html) how to install PostgreSql on a Mac. And, just do:
38
+
39
+ sudo gem install postgres-pr
40
+
41
+ to install postgreSql adapter.
42
+
43
+
44
+ Bugs
45
+ ----
46
+
47
+ Please report the issues through gitHub's issues.
48
+
49
+
50
+ Authors
51
+ -------
52
+ * [John Pignata](http://github.com/jpignata)
53
+ * [Anuj Dutta](http://github.com/andhapp) - [andHapp.com](http://www.andhapp.com/blog)
54
+
55
+
56
+ Roadmap
57
+ -------
58
+ * To make it work for any database extension. At the moment it would only for SQL extensions added by PostgreSql.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "tune_my_query"
5
+ gemspec.summary = "Standardises SQL syntax across databases."
6
+ gemspec.description = "Switches between standard SQL and extended SQL syntax depending on the adapter."
7
+ gemspec.email = "anuj@andhapp.com"
8
+ gemspec.homepage = "http://github.com/andhapp/tune_my_query"
9
+ gemspec.authors = ["Anuj Dutta"]
10
+ end
11
+ Jeweler::GemcutterTasks.new
12
+ rescue LoadError
13
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
14
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "lib/tune_my_query"
2
+
3
+ ActiveRecord::Base.send(:include, TuneMyQuery)
@@ -0,0 +1,15 @@
1
+ # LIKE COMMAND #
2
+ # Command for matching and substituting occurences of LIKE in the parameter given #
3
+ # Has one singleton method called 'execute' which performs the main function #
4
+ # #
5
+ class LikeCommand
6
+ class << self
7
+ def execute(condition)
8
+ if condition.is_a?(Array)
9
+ condition.each { |c| execute(c) } # Calling itself on each element if the supplied parameter is an Array
10
+ elsif condition.is_a?(String)
11
+ condition.gsub!(/ ilike /i, " like ") if condition.is_a?(String)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,49 @@
1
+ module TuneMyQuery
2
+
3
+ # Hook invoked in include
4
+ def self.included(base)
5
+ base.class_eval do
6
+ extend ClassMethods
7
+ class << self
8
+ alias_method :super_merge_conditions, :merge_conditions
9
+ alias_method :merge_conditions, :standardise_sql
10
+ end
11
+ end
12
+ end
13
+
14
+ # #
15
+ # Adds tune_my_query method to ActiveRecord and its subclasses. #
16
+ # #
17
+ # USAGE: #
18
+ # tune_my_query :like #
19
+ # #
20
+ # 'like' signifies the command to be invoked. #
21
+ # It also makes it apparent that an extension to LIKE needs to be standardise. Works only for #
22
+ # PostgreSQL specfific extensions. If MySql were to add its own extension to the standard SQL then #
23
+ # this gem would cease to work. #
24
+ # #
25
+ # METHODS: #
26
+ # #
27
+ # tune_my_query - Adds the command_type to to an instance variable namely, @command_type #
28
+ # #
29
+ # standardise_sql - Checks if PostgreSQLAdapter is used only then runs the conditions through the #
30
+ # Command. if not leaves the conditions untouched #
31
+ # #
32
+ # #
33
+ module ClassMethods
34
+ def tune_my_query(command_type)
35
+ @command_type = command_type
36
+ end
37
+
38
+ def standardise_sql(*conditions)
39
+ conditions = conditions.dup
40
+ @adapter = connection && !connection.class.to_s.include?("PostgreSQLAdapter")
41
+ command = "#{@command_type.to_s.camelize}Command".constantize
42
+ if @adapter
43
+ conditions.each { |condition| command.execute(condition) }
44
+ end
45
+ super_merge_conditions(*conditions)
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,4 @@
1
+ $: << File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require "tune_my_query/tune_my_query"
4
+ Dir.glob("#{File.dirname(__FILE__)}/tune_my_query/commands/*.*").each{|file| require file}
data/spec/database.yml ADDED
@@ -0,0 +1,12 @@
1
+ mysql:
2
+ :adapter: mysql
3
+ :database: tune_my_query
4
+ :username: root
5
+ :hostname: localhost
6
+
7
+ postgres:
8
+ :adapter: postgresql
9
+ :database: tune_my_query
10
+ :username: postgres
11
+ :password: postgres
12
+ :hostname: localhost
@@ -0,0 +1,46 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe "TuneMyQuery" do
4
+
5
+ shared_examples_for "a class that responds to tune_my_query method" do
6
+ it "should respond to method tune_my_query" do
7
+ Auberdine.should respond_to(:tune_my_query)
8
+ end
9
+ end
10
+
11
+ shared_examples_for "" do
12
+ end
13
+
14
+ context "When connected to MySql" do
15
+ before do
16
+ connect('mysql')
17
+ end
18
+
19
+ it_should_behave_like "a class that responds to tune_my_query method"
20
+
21
+ context "for LIKE SQL command" do
22
+ it "should convert from postgresql specific to standard SQL" do
23
+ Auberdine.merge_conditions(["name = 'joe'"], ["address ilike 'main'"]).
24
+ should be_eql("(name = 'joe') AND (address like 'main')")
25
+ end
26
+ end
27
+ end
28
+
29
+
30
+ context "When connected to PostgreSql" do
31
+ before do
32
+ connect('postgres')
33
+ end
34
+
35
+ it_should_behave_like "a class that responds to tune_my_query method"
36
+
37
+ context "for LIKE SQL command" do
38
+ it "should not convert from postgresql specific to standard SQL" do
39
+ Auberdine.merge_conditions(["name = 'joe'"], ["address ilike 'main'"]).
40
+ should be_eql("(name = 'joe') AND (address ilike 'main')")
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ end
data/spec/models.rb ADDED
@@ -0,0 +1,3 @@
1
+ class Auberdine < ActiveRecord::Base
2
+ tune_my_query :like
3
+ end
data/spec/schema.db ADDED
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define(:version => 1) do
2
+ create_table "auberdines", :force => true do |t|
3
+ t.string "name"
4
+ t.string "description"
5
+ t.datetime "created_at"
6
+ t.datetime "updated_at"
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "tune_my_query")
2
+
3
+ %w(activerecord active_support yaml spec).each{|path| require path }
4
+
5
+ %w(../init models).each{|path| require File.join(File.dirname(__FILE__), path) }
6
+
7
+ def connect(adapter)
8
+ fix_quote_ident_postgres_error if adapter == "postgres"
9
+ conf = YAML::load(File.open(File.dirname(__FILE__) + '/database.yml'))
10
+ ActiveRecord::Base.establish_connection(conf[adapter])
11
+ load(File.join(File.dirname(__FILE__), "schema.db"))
12
+ end
13
+
14
+ def fix_quote_ident_postgres_error
15
+ require "postgres"
16
+ def PGconn.quote_ident(name); %("#{name}") end
17
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tune_my_query
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anuj Dutta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-15 00:00:00 +05:30
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Switches between standard SQL and extended SQL syntax depending on the adapter.
17
+ email: anuj@andhapp.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.markdown
25
+ files:
26
+ - HISTORY
27
+ - LICENSE
28
+ - README.markdown
29
+ - Rakefile
30
+ - VERSION
31
+ - init.rb
32
+ - lib/tune_my_query.rb
33
+ - lib/tune_my_query/commands/LikeCommand.rb
34
+ - lib/tune_my_query/tune_my_query.rb
35
+ - spec/database.yml
36
+ - spec/lib/tune_my_query_spec.rb
37
+ - spec/models.rb
38
+ - spec/schema.db
39
+ - spec/spec_helper.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/andhapp/tune_my_query
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Standardises SQL syntax across databases.
68
+ test_files:
69
+ - spec/lib/tune_my_query_spec.rb
70
+ - spec/models.rb
71
+ - spec/spec_helper.rb