rebel 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rebel/sql.rb +73 -42
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea69d0d0787dec584bc554cb9fa7223937d55562
4
- data.tar.gz: c0d0f09f74a15f6079ac9496eb807981d3250edd
3
+ metadata.gz: aa3b3e1d946d65b40349085c1f02503587f2f3fd
4
+ data.tar.gz: e7923044d3c6f76a81eda0001941bb978adf85f5
5
5
  SHA512:
6
- metadata.gz: 01672fe5ec3f73beeca1e66465e70d19f47ed765c23f07c47f7dca4fbdbb617900568c7643a694accb47660955b886539bc576e991573fa497a12c96883def84
7
- data.tar.gz: 6c7457efbe296284d8acb777b3c39df51dd53ed3b94d439f885dacde8dc0638411a465ff7d72d97b556ca72aaafa028234a8c14b189a5bf3e04ea58ae656990e
6
+ metadata.gz: 5992df606d5eba47645c9301729488d4114f1d4278542d7476521675aaed788427d7a6f4e9673a32bd88f1238a06ee3bfa7ea9f14d6136d7a49805846cae8516
7
+ data.tar.gz: d44f06d4f085bee06d81b3db9a609a34dfdba2e6d191c003e316eca27bc154c8c7ffaa57c962f28b6e1ed46177ad04344fc5aa5cf72c5e6f2620849c7fc193b4
data/lib/rebel/sql.rb CHANGED
@@ -6,49 +6,53 @@ module Rebel::SQL
6
6
  end
7
7
 
8
8
  def create_table(table_name, desc)
9
- exec(SQL.create_table(table_name, desc))
9
+ exec(Rebel::SQL.create_table(table_name, desc))
10
+ end
11
+
12
+ def drop_table(table_name)
13
+ exec(Rebel::SQL.drop_table(table_name))
10
14
  end
11
15
 
12
16
  def select(*fields, from: nil, where: nil, inner: nil, left: nil, right: nil)
13
- exec(SQL.select(*fields,
14
- from: from,
15
- where: where,
16
- inner: inner,
17
- left: left,
18
- right: right))
17
+ exec(Rebel::SQL.select(*fields,
18
+ from: from,
19
+ where: where,
20
+ inner: inner,
21
+ left: left,
22
+ right: right))
19
23
  end
20
24
 
21
25
  def insert_into(table_name, *rows)
22
- exec(SQL.insert_into(table_name, *rows))
26
+ exec(Rebel::SQL.insert_into(table_name, *rows))
23
27
  end
24
28
 
25
29
  def update(table_name, set: nil, where: nil, inner: nil, left: nil, right: nil)
26
- exec(SQL.update(table_name, set: set, where: where, inner: inner, left: left, right: right))
30
+ exec(Rebel::SQL.update(table_name, set: set, where: where, inner: inner, left: left, right: right))
27
31
  end
28
32
 
29
33
  def delete_from(table_name, where: nil, inner: nil, left: nil, right: nil)
30
- exec(SQL.delete_from(table_name, where: where, inner: inner, left: left, right: right))
34
+ exec(Rebel::SQL.delete_from(table_name, where: where, inner: inner, left: left, right: right))
31
35
  end
32
36
 
33
37
  def truncate(table_name)
34
- exec(SQL.truncate(table_name))
38
+ exec(Rebel::SQL.truncate(table_name))
35
39
  end
36
40
 
37
41
  def count(*n)
38
- SQL.count(*n)
42
+ Rebel::SQL.count(*n)
39
43
  end
40
44
 
41
45
  def join(table, on: nil)
42
- SQL.join(table, on: on)
46
+ Rebel::SQL.join(table, on: on)
43
47
  end
44
48
 
45
49
  def outer_join(table, on: nil)
46
- SQL.outer_join(table, on: on)
50
+ Rebel::SQL.outer_join(table, on: on)
47
51
  end
48
52
 
49
53
  class Raw < String
50
54
  def as(n)
51
- Raw.new(self + " AS #{SQL.name(n)}")
55
+ Raw.new(self + " AS #{Rebel::SQL.name(n)}")
52
56
  end
53
57
 
54
58
  def as?(n)
@@ -56,7 +60,7 @@ module Rebel::SQL
56
60
  end
57
61
 
58
62
  def on(clause)
59
- Raw.new(self + " ON #{SQL.and_clause(clause)}")
63
+ Raw.new(self + " ON #{Rebel::SQL.and_clause(clause)}")
60
64
  end
61
65
 
62
66
  def on?(clause)
@@ -71,55 +75,61 @@ module Rebel::SQL
71
75
 
72
76
  def create_table(table_name, desc)
73
77
  <<-SQL
74
- CREATE TABLE #{SQL.name(table_name)} (
75
- #{SQL.list(desc.map { |k, v| "#{SQL.name(k)} #{v}" })}
78
+ CREATE TABLE #{Rebel::SQL.name(table_name)} (
79
+ #{Rebel::SQL.list(desc.map { |k, v| "#{Rebel::SQL.name(k)} #{v}" })}
76
80
  );
77
81
  SQL
78
82
  end
79
83
 
84
+ def drop_table(table_name)
85
+ <<-SQL
86
+ DROP TABLE #{Rebel::SQL.name(table_name)};
87
+ SQL
88
+ end
89
+
80
90
  def select(*fields, from: nil, where: nil, inner: nil, left: nil, right: nil)
81
91
  <<-SQL
82
92
  SELECT #{names(*fields)} FROM #{name(from)}
83
- #{SQL.inner?(inner)}
84
- #{SQL.left?(left)}
85
- #{SQL.right?(right)}
86
- #{SQL.where?(where)};
93
+ #{Rebel::SQL.inner?(inner)}
94
+ #{Rebel::SQL.left?(left)}
95
+ #{Rebel::SQL.right?(right)}
96
+ #{Rebel::SQL.where?(where)};
87
97
  SQL
88
98
  end
89
99
 
90
100
  def insert_into(table_name, *rows)
91
101
  <<-SQL
92
- INSERT INTO #{SQL.name(table_name)} (#{SQL.names(*rows.first.keys)})
93
- VALUES #{SQL.list(rows.map { |r| "(#{SQL.values(*r.values)})" })};
102
+ INSERT INTO #{Rebel::SQL.name(table_name)} (#{Rebel::SQL.names(*rows.first.keys)})
103
+ VALUES #{Rebel::SQL.list(rows.map { |r| "(#{Rebel::SQL.values(*r.values)})" })};
94
104
  SQL
95
105
  end
96
106
 
97
107
  def update(table_name, set: nil, where: nil, inner: nil, left: nil, right: nil)
98
- fail ArgumentError if set.nil?
108
+ raise ArgumentError if set.nil?
99
109
 
100
110
  <<-SQL
101
- UPDATE #{SQL.name(table_name)}
102
- SET #{SQL.assign_clause(set)}
103
- #{SQL.inner?(inner)}
104
- #{SQL.left?(left)}
105
- #{SQL.right?(right)}
106
- #{SQL.where?(where)};
111
+ UPDATE #{Rebel::SQL.name(table_name)}
112
+ SET #{Rebel::SQL.assign_clause(set)}
113
+ #{Rebel::SQL.inner?(inner)}
114
+ #{Rebel::SQL.left?(left)}
115
+ #{Rebel::SQL.right?(right)}
116
+ #{Rebel::SQL.where?(where)};
107
117
  SQL
108
118
  end
109
119
 
110
120
  def delete_from(table_name, where: nil, inner: nil, left: nil, right: nil)
111
121
  <<-SQL
112
- DELETE FROM #{SQL.name(table_name)}
113
- #{SQL.inner?(inner)}
114
- #{SQL.left?(left)}
115
- #{SQL.right?(right)}
116
- #{SQL.where?(where)};
122
+ DELETE FROM #{Rebel::SQL.name(table_name)}
123
+ #{Rebel::SQL.inner?(inner)}
124
+ #{Rebel::SQL.left?(left)}
125
+ #{Rebel::SQL.right?(right)}
126
+ #{Rebel::SQL.where?(where)};
117
127
  SQL
118
128
  end
119
129
 
120
130
  def truncate(table_name)
121
131
  <<-SQL
122
- TRUNCATE #{SQL.name(table_name)};
132
+ TRUNCATE #{Rebel::SQL.name(table_name)};
123
133
  SQL
124
134
  end
125
135
 
@@ -137,6 +147,18 @@ module Rebel::SQL
137
147
  raw("OUTER JOIN #{name(table)}").on?(on)
138
148
  end
139
149
 
150
+ def inner_join(table, on: nil)
151
+ raw(inner? join(table, on: on))
152
+ end
153
+
154
+ def left_outer_join(table, on: nil)
155
+ raw(left? outer_join(table, on: on))
156
+ end
157
+
158
+ def right_outer_join(table, on: nil)
159
+ raw(right? outer_join(table, on: on))
160
+ end
161
+
140
162
  ## Support
141
163
 
142
164
  def name(name)
@@ -158,9 +180,9 @@ module Rebel::SQL
158
180
  case v
159
181
  when Raw then v
160
182
  when String then raw "'#{v.tr("'", "''")}'"
161
- when Fixnum then raw v.to_s
183
+ when Integer then raw v.to_s
162
184
  when nil then raw 'NULL'
163
- else fail NotImplementedError, v.inspect
185
+ else raise NotImplementedError, v.inspect
164
186
  end
165
187
  end
166
188
 
@@ -180,14 +202,23 @@ module Rebel::SQL
180
202
  list(clause.map { |k, v| equal(k, v) })
181
203
  end
182
204
 
205
+ def clause_term(left, right)
206
+ case right
207
+ when Array
208
+ "#{name(left)} IN (#{values(*right)})"
209
+ else
210
+ "#{name(left)} = #{name_or_value(right)}"
211
+ end
212
+ end
213
+
183
214
  def and_clause(clause)
184
215
  return clause if clause.is_a?(Raw) || clause.is_a?(String)
185
216
 
186
217
  clause.map do |e|
187
218
  case e
188
- when Array then "#{name(e[0])} = #{name_or_value(e[1])}"
219
+ when Array then clause_term(e[0], e[1])
189
220
  when Raw, String then e
190
- else fail NotImplementedError, e.class
221
+ else raise NotImplementedError, e.class
191
222
  end
192
223
  end.join(' AND ')
193
224
  end
@@ -195,7 +226,7 @@ module Rebel::SQL
195
226
  def where?(clause)
196
227
  return "WHERE #{clause}" if clause.is_a?(Raw) || clause.is_a?(String)
197
228
 
198
- (clause && clause.any?) ? "WHERE #{SQL.and_clause(clause)}" : nil
229
+ clause && clause.any? ? "WHERE #{Rebel::SQL.and_clause(clause)}" : nil
199
230
  end
200
231
 
201
232
  def inner?(join)
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rebel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loic Nageleisen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-27 00:00:00.000000000 Z
11
+ date: 2017-03-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Write SQL queries in Ruby, or is it the other way around?
13
+ description: SQL-flavoured Ruby, or is it the other way around?
14
14
  email: loic.nageleisen@gmail.com
15
15
  executables: []
16
16
  extensions: []