sqb 1.0.7 → 1.0.8

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
  SHA1:
3
- metadata.gz: c682c36dc27d41519663b1caf06a97edd9cfdb6c
4
- data.tar.gz: 92637092f8f29020c72d40b466b4acfbd43e9060
3
+ metadata.gz: 62ef6126a194c6379a78a024567b91a2278d57cf
4
+ data.tar.gz: 74173493a093a294043c3f672079d64a58caa810
5
5
  SHA512:
6
- metadata.gz: cd376b13686ee0f7f27621c275cb52100bf9872de3df5cc9805e936f0c591f4ffea321e1076eb4a00b1fdb4a50949cb1c56fa1baa6e2db691f4716606c91f58e
7
- data.tar.gz: f1e60e3a689895eb05c5305dc9fb803bb7d088281ce07ceef062222515e6e82151c04e19be92b6bb19ff269f4eff41484622214e1cad2f28d826406387832a8a
6
+ metadata.gz: a9493b63fe326d3375ef2f4399ff623083da10e062e8697cf498d7c221793efa2919f601af7ea9ebf39ed282b98b24c72a1f82104a19b4f727c271324a0052d2
7
+ data.tar.gz: 1113bf3ae5beb73686c636382578bb7acb503c50bb143273417a353c3de3928ee280d973b3002733c9c5200dd088dce335a0c2566154505618dd1b87add02ea8
data/lib/sqb/base.rb CHANGED
@@ -7,6 +7,7 @@ module SQB
7
7
  include SQB::Escaping
8
8
 
9
9
  attr_reader :prepared_arguments
10
+ attr_reader :options
10
11
 
11
12
  def initialize(table_name, options = {}, &block)
12
13
  @table_name = table_name
data/lib/sqb/escaping.rb CHANGED
@@ -33,8 +33,12 @@ module SQB
33
33
  elsif value.is_a?(SQB::SafeString)
34
34
  value.to_s
35
35
  else
36
- @prepared_arguments << value.to_s
37
- '?'
36
+ if @options[:escaper]
37
+ @options[:escaper].call(value.to_s)
38
+ else
39
+ @prepared_arguments << value.to_s
40
+ '?'
41
+ end
38
42
  end
39
43
  end
40
44
 
data/lib/sqb/filtering.rb CHANGED
@@ -28,6 +28,10 @@ module SQB
28
28
  # Set that all conditions added in this block should be joined using OR
29
29
  # rather than AND.
30
30
  def or(&block)
31
+ if @where_within_or.is_a?(Array)
32
+ raise QueryError, "Cannot nest an or block within another or block"
33
+ end
34
+
31
35
  @where_within_or ||= []
32
36
  # Start by making an array within the OR block for this calling
33
37
  @where_within_or << []
data/lib/sqb/insert.rb CHANGED
@@ -5,14 +5,15 @@ module SQB
5
5
 
6
6
  def to_sql
7
7
  [].tap do |query|
8
- query << "INSERT INTO"
8
+ values_sql = values.map { |rec| "(" + rec.join(', ') + ")" }.join(', ')
9
+ query << "#{mysql_verb} INTO"
9
10
  query << escape_and_join(@options[:database_name], @table_name)
10
- if @values.nil? || @values.empty?
11
+ if values_sql.empty?
11
12
  raise NoValuesError, "No values have been specified. Use `value` to add values to the query."
12
13
  end
13
14
  query << "(#{columns.join(', ')})"
14
15
  query << "VALUES"
15
- query << "(#{values.join(', ')})"
16
+ query << values_sql
16
17
  end.join(' ')
17
18
  end
18
19
 
@@ -21,22 +22,52 @@ module SQB
21
22
  # @param key [String]
22
23
  # @param value [String, nil]
23
24
  def value(hash)
24
- @values ||= {}
25
+ if @record.nil?
26
+ record = (@local_record ||= {})
27
+ else
28
+ record = @record
29
+ end
30
+
25
31
  hash.each do |key, value|
26
- @values[key] = value
32
+ record[key] = value
27
33
  end
34
+
28
35
  self
29
36
  end
30
37
  alias_method :values, :value
31
38
 
39
+ def record(&block)
40
+ @record = {}
41
+ block.call
42
+ @records ||= []
43
+ @records << @record
44
+ @record = nil
45
+ end
46
+
32
47
  private
33
48
 
34
49
  def columns
35
- @values.keys.map { |k| escape(k) }
50
+ columns_keys.map { |c| escape(c) }
51
+ end
52
+
53
+ def columns_keys
54
+ all_records.map(&:keys).flatten.uniq
36
55
  end
37
56
 
38
57
  def values
39
- @values.values.map { |v| value_escape(v) }
58
+ all_records.map do |record|
59
+ columns_keys.map do |col|
60
+ value_escape(record[col])
61
+ end
62
+ end
63
+ end
64
+
65
+ def all_records
66
+ [@local_record, *@records || []].compact
67
+ end
68
+
69
+ def mysql_verb
70
+ "INSERT"
40
71
  end
41
72
 
42
73
  end
@@ -0,0 +1,64 @@
1
+ module SQB
2
+ class QueryString
3
+
4
+ def initialize(&block)
5
+ @query = []
6
+ block.call(self) if block_given?
7
+ end
8
+
9
+ def to_s
10
+ @query.join(" ")
11
+ end
12
+
13
+ def add(*items)
14
+ if @group_joiner
15
+ @group_items ||= []
16
+ @group_items << items
17
+ else
18
+ items.flatten.each do |item|
19
+ add_to_query(resolve_for_query(item))
20
+ end
21
+ end
22
+ end
23
+
24
+ def group(joiner, &block)
25
+ @group_joiner = joiner
26
+ block.call
27
+ ensure
28
+ if @group_items
29
+ # If we have some group items after running our group,
30
+ # add them into the query now.
31
+ @group_items.each do |items|
32
+ raw_q = items.map { |i| resolve_for_query(i) }.join(' ')
33
+ raw_q << @group_joiner unless items === @group_items.last
34
+ add_to_query(raw_q)
35
+ end
36
+ end
37
+ @group_items = nil
38
+ @group_joiner = nil
39
+ end
40
+
41
+ def with_brackets(&block)
42
+ @open_bracket = true
43
+ block.call
44
+ ensure
45
+ @query.last << ")"
46
+ @open_bracket = nil
47
+ @close_bracket = nil
48
+ end
49
+
50
+ private
51
+
52
+ def add_to_query(item)
53
+ item = "(" + item if @open_bracket
54
+ @query << item.to_s
55
+ ensure
56
+ @open_bracket = nil
57
+ end
58
+
59
+ def resolve_for_query(item)
60
+ item
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,13 @@
1
+ require 'sqb/insert'
2
+
3
+ module SQB
4
+ class Replace < Insert
5
+
6
+ private
7
+
8
+ def mysql_verb
9
+ "REPLACE"
10
+ end
11
+
12
+ end
13
+ end
data/lib/sqb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module SQB
2
- VERSION = '1.0.7'
2
+ VERSION = '1.0.8'
3
3
  end
data/lib/sqb.rb CHANGED
@@ -6,6 +6,7 @@ require 'sqb/select'
6
6
  require 'sqb/update'
7
7
  require 'sqb/delete'
8
8
  require 'sqb/insert'
9
+ require 'sqb/replace'
9
10
 
10
11
  module SQB
11
12
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-03 00:00:00.000000000 Z
11
+ date: 2018-12-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A friendly SQL builder for MySQL.
14
14
  email:
@@ -32,6 +32,8 @@ files:
32
32
  - lib/sqb/offsetting.rb
33
33
  - lib/sqb/ordering.rb
34
34
  - lib/sqb/query.rb
35
+ - lib/sqb/query_string.rb
36
+ - lib/sqb/replace.rb
35
37
  - lib/sqb/safe_string.rb
36
38
  - lib/sqb/select.rb
37
39
  - lib/sqb/update.rb