rusql 1.0.0 → 1.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a381919b48060b9765e153ac4ccad6cd4c76489f
4
- data.tar.gz: b9d6180ad19a5d30796b84211f89514e3336bc48
3
+ metadata.gz: 3adb1f9c5efd4c5bcd9b68068b29cd72feb2ab20
4
+ data.tar.gz: 9bffc7fe874eeaeae32f01a64d626d1ff112268c
5
5
  SHA512:
6
- metadata.gz: e60d9ca5b6a00ed5db2fe942e10d61924a48f7b308869a766f15c1db9179c0adc199de170d9ac422de430076bada47f1fde991dd4879e5c48f87780754ab9de1
7
- data.tar.gz: 27d0646cc1ec66b7e43126e86343211753d0090acd99d1d0567a6521a301fef1fc8b2f84f6926c038690aa0a6ac3ceac546b85090cf06429686673c969f4451a
6
+ metadata.gz: 41d27ede4646bb3635e26a27d41de33a70170477325eea98094ed222de8b9ef176de857514c73276e00bcb32d56f699259641b084a6da7c23ad980e58a935d67
7
+ data.tar.gz: e7f6cf7008ad2a8aaf4c3226ef4dcdca1b9a8abf9403633a2a478a4ce4d2458bc17b16b80b1746a67e1a1d7dc4fcde6bc86af3e805f1780871ea056d53ca5505
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.gem
data/README.md CHANGED
@@ -1,9 +1,5 @@
1
1
  # Rusql
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rusql`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
3
  ## Installation
8
4
 
9
5
  Add this line to your application's Gemfile:
@@ -22,13 +18,34 @@ Or install it yourself as:
22
18
 
23
19
  ## Usage
24
20
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
21
+ Sample code:
28
22
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
23
+ ```ruby
24
+ class User < ActiveRecord::Base
25
+ extend Rusql
26
+
27
+ def self.fetch(id:)
28
+ users = table(:users)
29
+ groups = table(:groups)
30
+ group_users = table(:group_users)
31
+
32
+ query = select(
33
+ users[:*],
34
+ group_concat( groups[:name] ).as(:group_names)
35
+ ).
36
+ from( users ).
37
+ left_outer_join( group_users, group_users[:user_id].equals( user[:id] ) ).
38
+ left_outer_join( groups, groups[:identifier].equals( group_users[:group_id] ) ).
39
+ where(
40
+ users[:id].equals(id).
41
+ and( groups[:created_at].greater_than( 7.days.ago ) )
42
+ ).
43
+ limit(1)
44
+
45
+ User.find_by_sql( query.to_s ).first
46
+ end
47
+ end
48
+ ```
32
49
 
33
50
  ## Contributing
34
51
 
data/lib/rusql/query.rb CHANGED
@@ -1,12 +1,5 @@
1
1
  module Rusql
2
2
  class Query
3
- attr_reader :selectors
4
- attr_reader :joins
5
- attr_reader :from_table
6
- attr_reader :condition
7
- attr_reader :orders
8
- attr_reader :limit
9
-
10
3
  def initialize(selectors)
11
4
  selectors.each do |selector|
12
5
  raise TypeException.new(Selector, selector.class) unless selector.is_a?(Selector)
@@ -17,62 +10,75 @@ module Rusql
17
10
  @orders = []
18
11
  end
19
12
 
13
+ def duplicate
14
+ new_one = Query.new(self.instance_variable_get(:@selectors))
15
+ new_one.instance_variable_set( :@condition, self.instance_variable_get(:@condition) )
16
+ new_one.instance_variable_set( :@from_table, self.instance_variable_get(:@from_table) )
17
+ new_one.instance_variable_set( :@joins, self.instance_variable_get(:@joins) )
18
+ new_one.instance_variable_set( :@limit, self.instance_variable_get(:@limit) )
19
+ new_one.instance_variable_set( :@orders, self.instance_variable_get(:@orders) )
20
+
21
+ new_one
22
+ end
23
+
20
24
  def select(*selectors)
21
25
  selectors.each do |selector|
22
26
  raise TypeException.new(Selector, selector.class) unless selector.is_a?(Selector)
23
27
  end
24
28
 
25
- @selectors = selectors
29
+ new_one = self.duplicate
30
+ new_one.instance_variable_set(:@selectors, selectors)
26
31
 
27
- self
32
+ new_one
28
33
  end
29
34
 
30
35
  def limit(c)
31
- raise TypeException unless c.is_a?(Fixnum)
36
+ raise TypeException.new(Fixnum, c.class) unless c.is_a?(Fixnum)
32
37
 
33
- @limit = c
38
+ new_one = self.duplicate
39
+ new_one.instance_variable_set(:@limit, c)
34
40
 
35
- self
41
+ new_one
36
42
  end
37
43
 
38
44
  def from(t)
39
- raise TypeException unless t.is_a?(Table)
45
+ raise TypeException.new(Table, t.class) unless t.is_a?(Table)
40
46
 
41
- @from_table = t
47
+ new_one = self.duplicate
48
+ new_one.instance_variable_set(:@from_table, t)
42
49
 
43
- self
50
+ new_one
44
51
  end
45
52
 
46
- def inner_join(table,condition)
47
- self.joins << Join.new(:inner_join, table, condition)
48
-
49
- self
50
- end
51
-
52
- def outer_join(table,condition)
53
- self.joins << Join.new(:outer_join, table, condition)
54
-
55
- self
56
- end
53
+ def join(join)
54
+ raise TypeException.new(Join, join.class) unless join.is_a?(Join)
57
55
 
58
- def left_outer_join(table,condition)
59
- self.joins << Join.new(:left_outer_join, table, condition)
56
+ new_one = self.duplicate
57
+ joins = new_one.instance_variable_get(:@joins)
58
+ joins << join
59
+ new_one.instance_variable_set(:@joins, joins)
60
60
 
61
- self
61
+ new_one
62
62
  end
63
63
 
64
- def right_outer_join(table,condition)
65
- self.joins << Join.new(:right_outer_join, table, condition)
64
+ %i(inner_join outer_join left_outer_join right_outer_join).each do |jm|
65
+ define_method jm, Proc.new { |table, condition|
66
+ new_one = self.duplicate
67
+ joins = new_one.instance_variable_get(:@joins)
68
+ joins << Join.new(jm, table, condition)
69
+ new_one.instance_variable_set(:@joins, joins)
66
70
 
67
- self
71
+ new_one
72
+ }
68
73
  end
69
74
 
70
75
  def where(condition)
71
76
  raise TypeException.new(Condition, condition.class) unless condition.is_a?(Condition)
72
77
 
73
- @condition = condition
78
+ new_one = self.duplicate
79
+ new_one.instance_variable_set(:@condition, condition)
74
80
 
75
- self
81
+ new_one
76
82
  end
77
83
 
78
84
  def order_by(*orders)
@@ -80,28 +86,29 @@ module Rusql
80
86
  raise TypeException.new(Order, o.class) unless o.is_a?(Order)
81
87
  end
82
88
 
83
- @orders = orders
89
+ new_one = self.duplicate
90
+ new_one.instance_variable_set(:@orders, orders)
84
91
 
85
- self
92
+ new_one
86
93
  end
87
94
 
88
95
  def to_s
89
- join_part = self.joins.map{ |j| "\n#{j.to_s}" }.join
96
+ join_part = @joins.map{ |j| "\n#{j.to_s}" }.join
90
97
  where_part = "\nWHERE"
91
- order_part = "\nORDER BY #{ self.orders.map(&:to_s).join(", ") }"
98
+ order_part = "\nORDER BY #{ @orders.map(&:to_s).join(", ") }"
92
99
 
93
- if self.condition.is_a?(BasicCondition)
100
+ if @condition.is_a?(BasicCondition)
94
101
  where_part += " "
95
- where_part += self.condition.to_s
96
- elsif self.condition.is_a?(ComplexCondition)
102
+ where_part += @condition.to_s
103
+ elsif @condition.is_a?(ComplexCondition)
97
104
  where_part += "\n "
98
- where_part += self.condition.to_s
105
+ where_part += @condition.to_s
99
106
  end
100
107
 
101
108
  <<-EOS
102
109
  SELECT
103
- #{ self.selectors.map{ |s| " #{s.to_s}" }.join(",\n") }
104
- FROM #{ self.from_table.to_s_for_aliasing }#{ (self.joins.length > 0) ? join_part : "" }#{ self.condition.nil? ? "" : where_part }#{ self.orders.length > 0 ? order_part : "" }
110
+ #{ @selectors.map{ |s| " #{s.to_s}" }.join(",\n") }
111
+ FROM #{ @from_table.to_s_for_aliasing }#{ (@joins.length > 0) ? join_part : "" }#{ @condition.nil? ? "" : where_part }#{ @orders.length > 0 ? order_part : "" }
105
112
  EOS
106
113
  end
107
114
  end
data/lib/rusql/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rusql
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rusql
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ajith Hussain