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 +4 -4
- data/.gitignore +1 -0
- data/README.md +27 -10
- data/lib/rusql/query.rb +51 -44
- data/lib/rusql/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3adb1f9c5efd4c5bcd9b68068b29cd72feb2ab20
|
4
|
+
data.tar.gz: 9bffc7fe874eeaeae32f01a64d626d1ff112268c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41d27ede4646bb3635e26a27d41de33a70170477325eea98094ed222de8b9ef176de857514c73276e00bcb32d56f699259641b084a6da7c23ad980e58a935d67
|
7
|
+
data.tar.gz: e7f6cf7008ad2a8aaf4c3226ef4dcdca1b9a8abf9403633a2a478a4ce4d2458bc17b16b80b1746a67e1a1d7dc4fcde6bc86af3e805f1780871ea056d53ca5505
|
data/.gitignore
CHANGED
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
|
-
|
26
|
-
|
27
|
-
## Development
|
21
|
+
Sample code:
|
28
22
|
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
29
|
+
new_one = self.duplicate
|
30
|
+
new_one.instance_variable_set(:@selectors, selectors)
|
26
31
|
|
27
|
-
|
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
|
-
|
38
|
+
new_one = self.duplicate
|
39
|
+
new_one.instance_variable_set(:@limit, c)
|
34
40
|
|
35
|
-
|
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
|
-
|
47
|
+
new_one = self.duplicate
|
48
|
+
new_one.instance_variable_set(:@from_table, t)
|
42
49
|
|
43
|
-
|
50
|
+
new_one
|
44
51
|
end
|
45
52
|
|
46
|
-
def
|
47
|
-
|
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
|
-
|
59
|
-
|
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
|
-
|
61
|
+
new_one
|
62
62
|
end
|
63
63
|
|
64
|
-
|
65
|
-
|
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
|
-
|
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
|
-
|
78
|
+
new_one = self.duplicate
|
79
|
+
new_one.instance_variable_set(:@condition, condition)
|
74
80
|
|
75
|
-
|
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
|
-
|
89
|
+
new_one = self.duplicate
|
90
|
+
new_one.instance_variable_set(:@orders, orders)
|
84
91
|
|
85
|
-
|
92
|
+
new_one
|
86
93
|
end
|
87
94
|
|
88
95
|
def to_s
|
89
|
-
join_part =
|
96
|
+
join_part = @joins.map{ |j| "\n#{j.to_s}" }.join
|
90
97
|
where_part = "\nWHERE"
|
91
|
-
order_part = "\nORDER BY #{
|
98
|
+
order_part = "\nORDER BY #{ @orders.map(&:to_s).join(", ") }"
|
92
99
|
|
93
|
-
if
|
100
|
+
if @condition.is_a?(BasicCondition)
|
94
101
|
where_part += " "
|
95
|
-
where_part +=
|
96
|
-
elsif
|
102
|
+
where_part += @condition.to_s
|
103
|
+
elsif @condition.is_a?(ComplexCondition)
|
97
104
|
where_part += "\n "
|
98
|
-
where_part +=
|
105
|
+
where_part += @condition.to_s
|
99
106
|
end
|
100
107
|
|
101
108
|
<<-EOS
|
102
109
|
SELECT
|
103
|
-
#{
|
104
|
-
FROM #{
|
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