query_optimizer 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d2b176485f31c12e5917dc6f1f1c22b26dbf72c
4
- data.tar.gz: d425922bc775bc57e08e0e9eea682b76e1c37081
3
+ metadata.gz: a84bf981c1b8a7f3c07f43590478b70f7229a288
4
+ data.tar.gz: d8002be95bb78fd6e9abfb17a9581f5a47d1a373
5
5
  SHA512:
6
- metadata.gz: 9a16d8277643f429c342e42b325e347879a59a9435f0a2096d027d92f1ce123327e0149e9440293bbddc44552e104df9924c2976a3f6a5099138f4bf66453ffd
7
- data.tar.gz: 423a12edc6fdbaeb5d4430767fdd7497abc0d0ff72f153c0c6db98267b92d2a18c49f435286321ca5b289d144c5be8ede7b0ea05c10dcb3f9175e701f330b608
6
+ metadata.gz: 7f4dee4b8038e7e154d74d58cb7a40ca71751d1a640ff266a7c3e25ec1e8cde98e4514c4191a245fea7c6b2e1073aafd7b1c8e74320907ad8de3791b8150bc82
7
+ data.tar.gz: e53f70803071d10027112d704e82afcea065cbcab1b7f32f4d5b229e314972b72d22522aaf4db87b19f1f36a84f2e33a8725ca72330add95a1c925be99b87be5
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # QueryOptimizer
2
2
 
3
- TODO: Write a gem description
3
+ This is rails gem which will minimize query for many_to_many relationship.
4
+ Minimize query by query_optimizer just pass your model name as QueryOptimizer.optimize_query(Model1,Model2)
4
5
 
5
6
  ## Installation
6
7
 
@@ -20,11 +21,11 @@ Or install it yourself as:
20
21
 
21
22
  ## Usage
22
23
 
23
- TODO: Write usage instructions here
24
+ Refer https://github.com/akshaygoyal88/query_optimizer/blob/master/README.rdoc for usage
24
25
 
25
26
  ## Contributing
26
27
 
27
- 1. Fork it ( https://github.com/[my-github-username]/query_optimizer/fork )
28
+ 1. Fork it ( https://github.com/akshaygoyal88/query_optimizer/fork )
28
29
  2. Create your feature branch (`git checkout -b my-new-feature`)
29
30
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
31
  4. Push to the branch (`git push origin my-new-feature`)
data/README.rdoc CHANGED
@@ -34,7 +34,8 @@ so to minimize query you can use query_optimizer you can just pass your model na
34
34
  require 'query_optimizer'
35
35
  QueryOptimizer.optimize_query(Post,Comment)
36
36
 
37
- (1.1ms) SELECT posts.*,comments.* FROM posts INNER JOIN comments on posts.id=comments.post_id
37
+ (1.1ms) SELECT posts.id AS posts_id,posts.name AS posts_name,posts.description AS posts_description,posts.created_at AS posts_created_at,posts.updated_at AS posts_updated_at,comments.id AS comments_id,comments.name AS comments_name,comments.description AS comments_description,comments.post_id AS comments_post_id,comments.created_at AS comments_created_at,comments.updated_at AS comments_updated_at FROM posts, comments where posts.id=comments.post_id
38
+
38
39
  => [{"posts_id"=>44, "posts_name"=>"name1", "posts_description"=>"desc1", "posts_created_at"=>2015-02-01 10:40:05 UTC, "posts_updated_at"=>2015-02-01 10:40:05 UTC, "comments_id"=>4, "comments_name"=>"gdfdhddffdd", "comments_description"=>nil, "comments_post_id"=>44, "comments_created_at"=>2015-10-10 16:49:12 UTC, "comments_updated_at"=>2015-10-10 16:49:12 UTC}, {"posts_id"=>45, "posts_name"=>"name2", "posts_description"=>"desc2", "posts_created_at"=>2015-02-01 10:40:05 UTC, "posts_updated_at"=>2015-02-01 10:40:05 UTC, "comments_id"=>5, "comments_name"=>"dhdfdfhdhdfhfd", "comments_description"=>nil, "comments_post_id"=>45, "comments_created_at"=>2015-10-10 16:49:19 UTC, "comments_updated_at"=>2015-10-10 16:49:19 UTC}, {"posts_id"=>44, "posts_name"=>"name3", "posts_description"=>"desc3", "posts_created_at"=>2015-02-01 10:40:05 UTC, "posts_updated_at"=>2015-02-01 10:40:05 UTC, "comments_id"=>6, "comments_name"=>"hfdhdfhdf", "comments_description"=>nil, "comments_post_id"=>44, "comments_created_at"=>2015-10-10 16:51:26 UTC, "comments_updated_at"=>2015-10-10 16:51:26 UTC}, {"posts_id"=>46, "posts_name"=>"name3", "posts_description"=>"desc3", "posts_created_at"=>2015-02-01 10:41:14 UTC, "posts_updated_at"=>2015-02-01 10:41:14 UTC, "comments_id"=>7, "comments_name"=>"dhdfdfhdfhdf", "comments_description"=>nil, "comments_post_id"=>46, "comments_created_at"=>2015-10-10 16:51:32 UTC, "comments_updated_at"=>2015-10-10 16:51:32 UTC}]
39
40
 
40
41
  It will return posts and its related comments in a single array
@@ -1,3 +1,3 @@
1
1
  module QueryOptimizer
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -3,16 +3,14 @@ module QueryOptimizer
3
3
  table1 = model1.table_name
4
4
  table2 = model2.table_name
5
5
  get_singular_name = table1.singularize
6
- results = ActiveRecord::Base.connection.execute("SELECT #{table1}.*,#{table2}.* FROM #{table1} INNER JOIN #{table2} on #{table1}.id=#{table2}.#{get_singular_name}_id")
7
6
  table1_columns = model1.column_names
8
7
  table2_columns = model2.column_names
9
- table1_columns = table1_columns.map { |word| "#{table1}_#{word}" }
10
- table2_columns = table2_columns.map { |word| "#{table2}_#{word}" }
11
- both_columns = table1_columns+table2_columns
12
- results.to_a.map{|i| Hash[both_columns.zip(i)]}
8
+ table1alias = table1_columns.map { |word| "#{table1}.#{word} AS #{table1}_#{word}" }.join(',')
9
+ table2alias = table2_columns.map { |word| "#{table2}.#{word} AS #{table2}_#{word}" }.join(',')
10
+ results = ActiveRecord::Base.connection.select_all("SELECT #{table1alias},#{table2alias} FROM #{table1}, #{table2} where #{table1}.id=#{table2}.#{get_singular_name}_id").to_a
13
11
  end
14
12
  end
15
13
 
16
14
  class ActiveRecord::Base
17
15
  include QueryOptimizer
18
- end
16
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: query_optimizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akshay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-18 00:00:00.000000000 Z
11
+ date: 2015-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler