sqb 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sqb/error.rb +9 -0
- data/lib/sqb/query.rb +20 -9
- data/lib/sqb/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2387d8f2e864013ed743ea61957668337b6845cf12ee726f21b6b8e5c780a27b
|
4
|
+
data.tar.gz: 346cf18dec4a83500e1b946e1ce0a8803343abb14de0be3b91d201672bd3bd2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 992277c06cef720684186e15492b6f48cc93cd18286054966392a65af711e5eb05b3e19eb16f0d0f9c82d0d0a4ea336eda23b1091ffbd33178846966863a27b9
|
7
|
+
data.tar.gz: 4f961eebaf2c818a6b3601bed4dafe4f089ad2cdffd6149fdef8af4047e1ead9ff28680a89e338b4c10fdd9b4c91dff16c012666c2fa956d618f830095432c67
|
data/lib/sqb/error.rb
CHANGED
data/lib/sqb/query.rb
CHANGED
@@ -3,9 +3,11 @@ require 'sqb/error'
|
|
3
3
|
module SQB
|
4
4
|
class Query
|
5
5
|
|
6
|
+
attr_reader :prepared_arguments
|
7
|
+
|
6
8
|
VALID_ORDERS = ['ASC', 'DESC']
|
7
9
|
|
8
|
-
def initialize(table_name, &escape_block)
|
10
|
+
def initialize(table_name, options = {}, &escape_block)
|
9
11
|
@table_name = table_name
|
10
12
|
@columns = []
|
11
13
|
@joins = []
|
@@ -17,7 +19,14 @@ module SQB
|
|
17
19
|
@offset = nil
|
18
20
|
@distinct = false
|
19
21
|
@where_within_or = []
|
20
|
-
@
|
22
|
+
@options = options
|
23
|
+
@prepared_arguments = []
|
24
|
+
|
25
|
+
if @options[:prepared] == false && escape_block.nil?
|
26
|
+
raise EscapeBlockMissingError, "An escape block must be provided if prepared statements are disabled."
|
27
|
+
else
|
28
|
+
@escape_block = escape_block
|
29
|
+
end
|
21
30
|
end
|
22
31
|
|
23
32
|
# Generate the full SQL query for this query.
|
@@ -133,6 +142,7 @@ module SQB
|
|
133
142
|
@where << "(#{@where_within_or_sql.flatten.join(' OR ')})"
|
134
143
|
@where_within_or_sql = nil
|
135
144
|
end
|
145
|
+
self
|
136
146
|
end
|
137
147
|
|
138
148
|
# Limit the number of records return
|
@@ -162,7 +172,7 @@ module SQB
|
|
162
172
|
direction = direction ? direction.to_s.upcase : 'ASC'
|
163
173
|
|
164
174
|
unless VALID_ORDERS.include?(direction)
|
165
|
-
raise
|
175
|
+
raise InvalidOrderDirectionError, "Invalid order direction #{direction}"
|
166
176
|
end
|
167
177
|
|
168
178
|
with_table_and_column(column) do |table, column|
|
@@ -290,7 +300,7 @@ module SQB
|
|
290
300
|
op = operator == :in ? "IN" : "NOT IN"
|
291
301
|
sql << "#{key} #{op} (#{escaped_values})"
|
292
302
|
else
|
293
|
-
raise
|
303
|
+
raise InvalidOperatorError, "Invalid operator '#{operator}'"
|
294
304
|
end
|
295
305
|
end
|
296
306
|
sql.empty? ? "1=0" : sql.join(joiner)
|
@@ -313,19 +323,20 @@ module SQB
|
|
313
323
|
|
314
324
|
def value_escape(value)
|
315
325
|
if value == true
|
316
|
-
|
326
|
+
1
|
317
327
|
elsif value == false
|
318
|
-
|
328
|
+
0
|
319
329
|
elsif value.nil?
|
320
330
|
'NULL'
|
321
331
|
elsif value.is_a?(Integer)
|
322
332
|
value.to_i
|
323
333
|
else
|
324
|
-
if
|
325
|
-
'NULL'
|
326
|
-
else
|
334
|
+
if @options[:prepared] == false
|
327
335
|
escaped_value = @escape_block ? @escape_block.call(value.to_s) : value.to_s
|
328
336
|
"'" + escaped_value + "'"
|
337
|
+
else
|
338
|
+
@prepared_arguments << value.to_s
|
339
|
+
'?'
|
329
340
|
end
|
330
341
|
end
|
331
342
|
end
|
data/lib/sqb/version.rb
CHANGED