rebel 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/rebel.rb +3 -0
- data/lib/rebel/sql.rb +213 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ea69d0d0787dec584bc554cb9fa7223937d55562
|
4
|
+
data.tar.gz: c0d0f09f74a15f6079ac9496eb807981d3250edd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 01672fe5ec3f73beeca1e66465e70d19f47ed765c23f07c47f7dca4fbdbb617900568c7643a694accb47660955b886539bc576e991573fa497a12c96883def84
|
7
|
+
data.tar.gz: 6c7457efbe296284d8acb777b3c39df51dd53ed3b94d439f885dacde8dc0638411a465ff7d72d97b556ca72aaafa028234a8c14b189a5bf3e04ea58ae656990e
|
data/lib/rebel.rb
ADDED
data/lib/rebel/sql.rb
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
module Rebel::SQL
|
2
|
+
attr_reader :conn
|
3
|
+
|
4
|
+
def exec(query)
|
5
|
+
conn.exec(query)
|
6
|
+
end
|
7
|
+
|
8
|
+
def create_table(table_name, desc)
|
9
|
+
exec(SQL.create_table(table_name, desc))
|
10
|
+
end
|
11
|
+
|
12
|
+
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))
|
19
|
+
end
|
20
|
+
|
21
|
+
def insert_into(table_name, *rows)
|
22
|
+
exec(SQL.insert_into(table_name, *rows))
|
23
|
+
end
|
24
|
+
|
25
|
+
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))
|
27
|
+
end
|
28
|
+
|
29
|
+
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))
|
31
|
+
end
|
32
|
+
|
33
|
+
def truncate(table_name)
|
34
|
+
exec(SQL.truncate(table_name))
|
35
|
+
end
|
36
|
+
|
37
|
+
def count(*n)
|
38
|
+
SQL.count(*n)
|
39
|
+
end
|
40
|
+
|
41
|
+
def join(table, on: nil)
|
42
|
+
SQL.join(table, on: on)
|
43
|
+
end
|
44
|
+
|
45
|
+
def outer_join(table, on: nil)
|
46
|
+
SQL.outer_join(table, on: on)
|
47
|
+
end
|
48
|
+
|
49
|
+
class Raw < String
|
50
|
+
def as(n)
|
51
|
+
Raw.new(self + " AS #{SQL.name(n)}")
|
52
|
+
end
|
53
|
+
|
54
|
+
def as?(n)
|
55
|
+
n ? as(n) : self
|
56
|
+
end
|
57
|
+
|
58
|
+
def on(clause)
|
59
|
+
Raw.new(self + " ON #{SQL.and_clause(clause)}")
|
60
|
+
end
|
61
|
+
|
62
|
+
def on?(clause)
|
63
|
+
clause ? on(clause) : self
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class << self
|
68
|
+
def raw(str)
|
69
|
+
Raw.new(str)
|
70
|
+
end
|
71
|
+
|
72
|
+
def create_table(table_name, desc)
|
73
|
+
<<-SQL
|
74
|
+
CREATE TABLE #{SQL.name(table_name)} (
|
75
|
+
#{SQL.list(desc.map { |k, v| "#{SQL.name(k)} #{v}" })}
|
76
|
+
);
|
77
|
+
SQL
|
78
|
+
end
|
79
|
+
|
80
|
+
def select(*fields, from: nil, where: nil, inner: nil, left: nil, right: nil)
|
81
|
+
<<-SQL
|
82
|
+
SELECT #{names(*fields)} FROM #{name(from)}
|
83
|
+
#{SQL.inner?(inner)}
|
84
|
+
#{SQL.left?(left)}
|
85
|
+
#{SQL.right?(right)}
|
86
|
+
#{SQL.where?(where)};
|
87
|
+
SQL
|
88
|
+
end
|
89
|
+
|
90
|
+
def insert_into(table_name, *rows)
|
91
|
+
<<-SQL
|
92
|
+
INSERT INTO #{SQL.name(table_name)} (#{SQL.names(*rows.first.keys)})
|
93
|
+
VALUES #{SQL.list(rows.map { |r| "(#{SQL.values(*r.values)})" })};
|
94
|
+
SQL
|
95
|
+
end
|
96
|
+
|
97
|
+
def update(table_name, set: nil, where: nil, inner: nil, left: nil, right: nil)
|
98
|
+
fail ArgumentError if set.nil?
|
99
|
+
|
100
|
+
<<-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)};
|
107
|
+
SQL
|
108
|
+
end
|
109
|
+
|
110
|
+
def delete_from(table_name, where: nil, inner: nil, left: nil, right: nil)
|
111
|
+
<<-SQL
|
112
|
+
DELETE FROM #{SQL.name(table_name)}
|
113
|
+
#{SQL.inner?(inner)}
|
114
|
+
#{SQL.left?(left)}
|
115
|
+
#{SQL.right?(right)}
|
116
|
+
#{SQL.where?(where)};
|
117
|
+
SQL
|
118
|
+
end
|
119
|
+
|
120
|
+
def truncate(table_name)
|
121
|
+
<<-SQL
|
122
|
+
TRUNCATE #{SQL.name(table_name)};
|
123
|
+
SQL
|
124
|
+
end
|
125
|
+
|
126
|
+
## Functions
|
127
|
+
|
128
|
+
def count(*n)
|
129
|
+
raw("COUNT(#{names(*n)})")
|
130
|
+
end
|
131
|
+
|
132
|
+
def join(table, on: nil)
|
133
|
+
raw("JOIN #{name(table)}").on?(on)
|
134
|
+
end
|
135
|
+
|
136
|
+
def outer_join(table, on: nil)
|
137
|
+
raw("OUTER JOIN #{name(table)}").on?(on)
|
138
|
+
end
|
139
|
+
|
140
|
+
## Support
|
141
|
+
|
142
|
+
def name(name)
|
143
|
+
return name if name.is_a?(Raw)
|
144
|
+
return raw('*') if name == '*'
|
145
|
+
|
146
|
+
name.to_s.split('.').map { |e| "\"#{e}\"" }.join('.')
|
147
|
+
end
|
148
|
+
|
149
|
+
def names(*names)
|
150
|
+
list(names.map { |k| name(k) })
|
151
|
+
end
|
152
|
+
|
153
|
+
def list(*items)
|
154
|
+
items.join(', ')
|
155
|
+
end
|
156
|
+
|
157
|
+
def value(v)
|
158
|
+
case v
|
159
|
+
when Raw then v
|
160
|
+
when String then raw "'#{v.tr("'", "''")}'"
|
161
|
+
when Fixnum then raw v.to_s
|
162
|
+
when nil then raw 'NULL'
|
163
|
+
else fail NotImplementedError, v.inspect
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def values(*values)
|
168
|
+
list(values.map { |v| value(v) })
|
169
|
+
end
|
170
|
+
|
171
|
+
def name_or_value(item)
|
172
|
+
item.is_a?(Symbol) ? name(item) : value(item)
|
173
|
+
end
|
174
|
+
|
175
|
+
def equal(l, r)
|
176
|
+
"#{name_or_value(l)} = #{name_or_value(r)}"
|
177
|
+
end
|
178
|
+
|
179
|
+
def assign_clause(clause)
|
180
|
+
list(clause.map { |k, v| equal(k, v) })
|
181
|
+
end
|
182
|
+
|
183
|
+
def and_clause(clause)
|
184
|
+
return clause if clause.is_a?(Raw) || clause.is_a?(String)
|
185
|
+
|
186
|
+
clause.map do |e|
|
187
|
+
case e
|
188
|
+
when Array then "#{name(e[0])} = #{name_or_value(e[1])}"
|
189
|
+
when Raw, String then e
|
190
|
+
else fail NotImplementedError, e.class
|
191
|
+
end
|
192
|
+
end.join(' AND ')
|
193
|
+
end
|
194
|
+
|
195
|
+
def where?(clause)
|
196
|
+
return "WHERE #{clause}" if clause.is_a?(Raw) || clause.is_a?(String)
|
197
|
+
|
198
|
+
(clause && clause.any?) ? "WHERE #{SQL.and_clause(clause)}" : nil
|
199
|
+
end
|
200
|
+
|
201
|
+
def inner?(join)
|
202
|
+
join ? "INNER #{join}" : nil
|
203
|
+
end
|
204
|
+
|
205
|
+
def left?(join)
|
206
|
+
join ? "LEFT #{join}" : nil
|
207
|
+
end
|
208
|
+
|
209
|
+
def right?(join)
|
210
|
+
join ? "RIGHT #{join}" : nil
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rebel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Loic Nageleisen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Write SQL queries in Ruby, or is it the other way around?
|
14
|
+
email: loic.nageleisen@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/rebel.rb
|
20
|
+
- lib/rebel/sql.rb
|
21
|
+
homepage: https://github.com/lloeki/rebel.git
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.6.10
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Fight against the Object tyranny
|
45
|
+
test_files: []
|