rebel 0.5.0 → 0.6.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 +53 -40
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 484cad6b2851562dd80774e3249577df2bc3a6f4
4
- data.tar.gz: ab2aff65c3a66184915f4fa396cc8c0d8bff42d0
3
+ metadata.gz: 27e8c591923ecfe74584376ddecb13b4eec403f8
4
+ data.tar.gz: 4f6a4579e5bc1278f4ea46a65aa56ea4914fb62e
5
5
  SHA512:
6
- metadata.gz: 82f4a246958333b03b0b201fd926dd62c18ff71d36ac8368b314f7c14d13f3f630f2cabca7ccd0ba7ace4c3f23b8a8e9e73e3ba6fb652768f50b79c4aa170de3
7
- data.tar.gz: 2ef7487ccb43443f5167ec6260ba850573c9e3ca1109c0959d5c4b7ee27ad77a1beb7d4747234686b935b2703e374b01d85e2fe81ca4bbf4dce0efcde6188e3c
6
+ metadata.gz: 9700bf78fff4f75db2c2a4f9a5bbd96dc59241ab9c53366d78ecb4e3272627d2104290131177d2d0da6c4ee87193921fddb8d0c6ede21a8eb9e97b8d711c3e90
7
+ data.tar.gz: 8236a9eeb4fdd8150906291cc0f8e8308903648455e59e8eba767833d9446164ae2c6c3d7ea697a770bd7e0541d143777c0208a53de45e810147be21233597a8
@@ -1,4 +1,4 @@
1
- module Rebel::SQL
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
- Raw.new("(#{self})")
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
- Raw.new(self + " AS #{Rebel::SQL.name(n)}")
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
- Raw.new(self + " ON #{Rebel::SQL.and_clause(*clause)}")
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
- Raw.new(self + " HAVING #{Rebel::SQL.and_clause(*clause)}")
96
+ sql.raw(self + " HAVING #{sql.and_clause(*clause)}")
95
97
  end
96
98
 
97
99
  def asc
98
- Raw.new(self + " ASC")
100
+ sql.raw(self + " ASC")
99
101
  end
100
102
 
101
103
  def desc
102
- Raw.new(self + " DESC")
104
+ sql.raw(self + " DESC")
103
105
  end
104
106
 
105
107
  def and(*clause)
106
- Raw.new("#{self.parens?} AND #{Rebel::SQL.and_clause(*clause)}")
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
- Raw.new("#{self} OR #{Rebel::SQL.and_clause(*clause)}").wants_parens!
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
- Raw.new("#{self} IS NULL")
120
+ sql.raw("#{self} IS NULL")
119
121
  else
120
- Raw.new("#{self} = #{Rebel::SQL.name_or_value(n)}")
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
- Raw.new("#{self} IS NOT NULL")
131
+ sql.raw("#{self} IS NOT NULL")
130
132
  else
131
- Raw.new("#{self} != #{Rebel::SQL.name_or_value(n)}")
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
- Raw.new("#{self} < #{Rebel::SQL.name_or_value(n)}")
140
+ sql.raw("#{self} < #{sql.name_or_value(n)}")
139
141
  end
140
142
  alias < lt
141
143
 
142
144
  def gt(n)
143
- Raw.new("#{self} > #{Rebel::SQL.name_or_value(n)}")
145
+ sql.raw("#{self} > #{sql.name_or_value(n)}")
144
146
  end
145
147
  alias > gt
146
148
 
147
149
  def le(n)
148
- Raw.new("#{self} <= #{Rebel::SQL.name_or_value(n)}")
150
+ sql.raw("#{self} <= #{sql.name_or_value(n)}")
149
151
  end
150
152
  alias <= le
151
153
 
152
154
  def ge(n)
153
- Raw.new("#{self} >= #{Rebel::SQL.name_or_value(n)}")
155
+ sql.raw("#{self} >= #{sql.name_or_value(n)}")
154
156
  end
155
157
  alias >= ge
156
158
 
157
159
  def in(*v)
158
- Raw.new("#{self} IN (#{Rebel::SQL.values(*v)})")
160
+ sql.raw("#{self} IN (#{sql.values(*v)})")
159
161
  end
160
162
 
161
163
  def not_in(*v)
162
- Raw.new("#{self} NOT IN (#{Rebel::SQL.values(*v)})")
164
+ sql.raw("#{self} NOT IN (#{sql.values(*v)})")
163
165
  end
164
166
 
165
167
  def like(n)
166
- Raw.new("#{self} LIKE #{Rebel::SQL.value(n)}")
168
+ sql.raw("#{self} LIKE #{sql.value(n)}")
167
169
  end
168
170
 
169
171
  def not_like(n)
170
- Raw.new("#{self} NOT LIKE #{Rebel::SQL.value(n)}")
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
- def string_quote=(str)
184
- @string_quote = str
185
- end
175
+ private
186
176
 
187
- def escaped_string_quote=(str)
188
- @escaped_string_quote = str
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.tr(@string_quote, @escaped_string_quote)
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 "'#{escape_str(v)}'"
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 ? 'TRUE' : 'FALSE')
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rebel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loic Nageleisen