rebel 0.5.0 → 0.6.0
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/lib/rebel/sql.rb +53 -40
- 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: 27e8c591923ecfe74584376ddecb13b4eec403f8
|
4
|
+
data.tar.gz: 4f6a4579e5bc1278f4ea46a65aa56ea4914fb62e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9700bf78fff4f75db2c2a4f9a5bbd96dc59241ab9c53366d78ecb4e3272627d2104290131177d2d0da6c4ee87193921fddb8d0c6ede21a8eb9e97b8d711c3e90
|
7
|
+
data.tar.gz: 8236a9eeb4fdd8150906291cc0f8e8308903648455e59e8eba767833d9446164ae2c6c3d7ea697a770bd7e0541d143777c0208a53de45e810147be21233597a8
|
data/lib/rebel/sql.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
module Rebel::
|
1
|
+
module Rebel::SQLQ
|
2
2
|
attr_reader :conn
|
3
3
|
|
4
4
|
def exec(query)
|
@@ -54,7 +54,9 @@ module Rebel::SQL
|
|
54
54
|
def outer_join(table, on: nil)
|
55
55
|
Rebel::SQL.outer_join(table, on: on)
|
56
56
|
end
|
57
|
+
end
|
57
58
|
|
59
|
+
module Rebel
|
58
60
|
class Raw < String
|
59
61
|
def wants_parens!
|
60
62
|
@wants_parens = true
|
@@ -67,7 +69,7 @@ module Rebel::SQL
|
|
67
69
|
end
|
68
70
|
|
69
71
|
def parens
|
70
|
-
|
72
|
+
sql.raw("(#{self})")
|
71
73
|
end
|
72
74
|
|
73
75
|
def parens?
|
@@ -75,7 +77,7 @@ module Rebel::SQL
|
|
75
77
|
end
|
76
78
|
|
77
79
|
def as(n)
|
78
|
-
|
80
|
+
sql.raw(self + " AS #{sql.name(n)}")
|
79
81
|
end
|
80
82
|
|
81
83
|
def as?(n)
|
@@ -83,7 +85,7 @@ module Rebel::SQL
|
|
83
85
|
end
|
84
86
|
|
85
87
|
def on(*clause)
|
86
|
-
|
88
|
+
sql.raw(self + " ON #{sql.and_clause(*clause)}")
|
87
89
|
end
|
88
90
|
|
89
91
|
def on?(*clause)
|
@@ -91,33 +93,33 @@ module Rebel::SQL
|
|
91
93
|
end
|
92
94
|
|
93
95
|
def having(*clause)
|
94
|
-
|
96
|
+
sql.raw(self + " HAVING #{sql.and_clause(*clause)}")
|
95
97
|
end
|
96
98
|
|
97
99
|
def asc
|
98
|
-
|
100
|
+
sql.raw(self + " ASC")
|
99
101
|
end
|
100
102
|
|
101
103
|
def desc
|
102
|
-
|
104
|
+
sql.raw(self + " DESC")
|
103
105
|
end
|
104
106
|
|
105
107
|
def and(*clause)
|
106
|
-
|
108
|
+
sql.raw("#{self.parens?} AND #{sql.and_clause(*clause)}")
|
107
109
|
end
|
108
110
|
alias & and
|
109
111
|
|
110
112
|
def or(*clause)
|
111
|
-
|
113
|
+
sql.raw("#{self} OR #{sql.and_clause(*clause)}").wants_parens!
|
112
114
|
end
|
113
115
|
alias | or
|
114
116
|
|
115
117
|
def eq(n)
|
116
118
|
case n
|
117
119
|
when nil
|
118
|
-
|
120
|
+
sql.raw("#{self} IS NULL")
|
119
121
|
else
|
120
|
-
|
122
|
+
sql.raw("#{self} = #{sql.name_or_value(n)}")
|
121
123
|
end
|
122
124
|
end
|
123
125
|
alias == eq
|
@@ -126,70 +128,60 @@ module Rebel::SQL
|
|
126
128
|
def ne(n)
|
127
129
|
case n
|
128
130
|
when nil
|
129
|
-
|
131
|
+
sql.raw("#{self} IS NOT NULL")
|
130
132
|
else
|
131
|
-
|
133
|
+
sql.raw("#{self} != #{sql.name_or_value(n)}")
|
132
134
|
end
|
133
135
|
end
|
134
136
|
alias != ne
|
135
137
|
alias is_not ne
|
136
138
|
|
137
139
|
def lt(n)
|
138
|
-
|
140
|
+
sql.raw("#{self} < #{sql.name_or_value(n)}")
|
139
141
|
end
|
140
142
|
alias < lt
|
141
143
|
|
142
144
|
def gt(n)
|
143
|
-
|
145
|
+
sql.raw("#{self} > #{sql.name_or_value(n)}")
|
144
146
|
end
|
145
147
|
alias > gt
|
146
148
|
|
147
149
|
def le(n)
|
148
|
-
|
150
|
+
sql.raw("#{self} <= #{sql.name_or_value(n)}")
|
149
151
|
end
|
150
152
|
alias <= le
|
151
153
|
|
152
154
|
def ge(n)
|
153
|
-
|
155
|
+
sql.raw("#{self} >= #{sql.name_or_value(n)}")
|
154
156
|
end
|
155
157
|
alias >= ge
|
156
158
|
|
157
159
|
def in(*v)
|
158
|
-
|
160
|
+
sql.raw("#{self} IN (#{sql.values(*v)})")
|
159
161
|
end
|
160
162
|
|
161
163
|
def not_in(*v)
|
162
|
-
|
164
|
+
sql.raw("#{self} NOT IN (#{sql.values(*v)})")
|
163
165
|
end
|
164
166
|
|
165
167
|
def like(n)
|
166
|
-
|
168
|
+
sql.raw("#{self} LIKE #{sql.value(n)}")
|
167
169
|
end
|
168
170
|
|
169
171
|
def not_like(n)
|
170
|
-
|
171
|
-
end
|
172
|
-
end
|
173
|
-
|
174
|
-
@identifier_quote = '"'
|
175
|
-
@string_quote = "'"
|
176
|
-
@escaped_string_quote = "''"
|
177
|
-
|
178
|
-
class << self
|
179
|
-
def identifier_quote=(str)
|
180
|
-
@identifier_quote = str
|
172
|
+
sql.raw("#{self} NOT LIKE #{sql.value(n)}")
|
181
173
|
end
|
182
174
|
|
183
|
-
|
184
|
-
@string_quote = str
|
185
|
-
end
|
175
|
+
private
|
186
176
|
|
187
|
-
def
|
188
|
-
@
|
177
|
+
def sql
|
178
|
+
@sql ||= Rebel::SQLQ
|
189
179
|
end
|
180
|
+
end
|
190
181
|
|
182
|
+
module SQLB
|
191
183
|
def raw(str)
|
192
|
-
Raw.new(str)
|
184
|
+
Raw.new(str).tap { |r| r.instance_variable_set(:@sql, self) }
|
193
185
|
end
|
194
186
|
|
195
187
|
def create_table(table_name, desc)
|
@@ -309,15 +301,15 @@ module Rebel::SQL
|
|
309
301
|
end
|
310
302
|
|
311
303
|
def escape_str(str)
|
312
|
-
str.
|
304
|
+
str.gsub(@string_quote, @escaped_string_quote)
|
313
305
|
end
|
314
306
|
|
315
307
|
def value(v)
|
316
308
|
case v
|
317
309
|
when Raw then v
|
318
|
-
when String then raw "
|
310
|
+
when String then raw "#{@string_quote}#{escape_str(v)}#{@string_quote}"
|
319
311
|
when Integer then raw v.to_s
|
320
|
-
when TrueClass, FalseClass then raw(v ?
|
312
|
+
when TrueClass, FalseClass then raw(v ? @true_literal : @false_literal)
|
321
313
|
when Date, Time, DateTime then value(v.iso8601)
|
322
314
|
when nil then raw 'NULL'
|
323
315
|
else raise NotImplementedError, "#{v.class}: #{v.inspect}"
|
@@ -398,3 +390,24 @@ module Rebel::SQL
|
|
398
390
|
end
|
399
391
|
end
|
400
392
|
end
|
393
|
+
|
394
|
+
module Rebel
|
395
|
+
def self.SQL(options = {}, &block)
|
396
|
+
sql = const_defined?(:SQL) && options.empty? ? SQL : Module.new do
|
397
|
+
@identifier_quote = options[:identifier_quote] || '"'
|
398
|
+
@string_quote = options[:string_quote] || "'"
|
399
|
+
@escaped_string_quote = options[:escaped_string_quote] || "''"
|
400
|
+
@true_literal = options[:true_literal] || 'TRUE'
|
401
|
+
@false_literal = options[:false_literal] || 'FALSE'
|
402
|
+
|
403
|
+
extend Rebel::SQLB
|
404
|
+
include Rebel::SQLQ
|
405
|
+
end
|
406
|
+
|
407
|
+
return sql.instance_eval(&block) unless block.nil?
|
408
|
+
|
409
|
+
sql
|
410
|
+
end
|
411
|
+
|
412
|
+
SQL = SQL()
|
413
|
+
end
|