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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44f58ba5485e199e6364aa99556e1a7b2ea8bb5520ff6f5059bff7dcc96df8c2
4
- data.tar.gz: 8ae30d22f80255a79f13e0a372c65e62666202ec1c85f8a270a1bba75da96c99
3
+ metadata.gz: 2387d8f2e864013ed743ea61957668337b6845cf12ee726f21b6b8e5c780a27b
4
+ data.tar.gz: 346cf18dec4a83500e1b946e1ce0a8803343abb14de0be3b91d201672bd3bd2d
5
5
  SHA512:
6
- metadata.gz: 2d4bfa140701dfa6de3528106148c238e9be0e025ed11a1c857b9c795e54734491a3d582f8aed3ab2b73c0aec14c00e4044aca92aa9b180d5f721388fd9bacb8
7
- data.tar.gz: 13405cee8bf220248ae53c9d6eb8aac63d142cbb7a9b9f8f33cccbed95b89857d0f31312b858ed57aebc5eeca7b03bbeca3d75abc02a266430f3730503b8276b
6
+ metadata.gz: 992277c06cef720684186e15492b6f48cc93cd18286054966392a65af711e5eb05b3e19eb16f0d0f9c82d0d0a4ea336eda23b1091ffbd33178846966863a27b9
7
+ data.tar.gz: 4f961eebaf2c818a6b3601bed4dafe4f089ad2cdffd6149fdef8af4047e1ead9ff28680a89e338b4c10fdd9b4c91dff16c012666c2fa956d618f830095432c67
data/lib/sqb/error.rb CHANGED
@@ -1,4 +1,13 @@
1
1
  module SQB
2
2
  class Error < StandardError
3
3
  end
4
+
5
+ class EscapeBlockMissingError < Error
6
+ end
7
+
8
+ class InvalidOrderDirectionError < Error
9
+ end
10
+
11
+ class InvalidOperatorError < Error
12
+ end
4
13
  end
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
- @escape_block = escape_block
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 Error, "Invalid order direction #{direction}"
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 Error, "Invalid operator '#{operator}'"
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
- '1'
326
+ 1
317
327
  elsif value == false
318
- '0'
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 value.to_s.length == 0
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
@@ -1,3 +1,3 @@
1
1
  module SQB
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke