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 +4 -4
- data/lib/sqb/base.rb +1 -0
- data/lib/sqb/escaping.rb +6 -2
- data/lib/sqb/filtering.rb +4 -0
- data/lib/sqb/insert.rb +38 -7
- data/lib/sqb/query_string.rb +64 -0
- data/lib/sqb/replace.rb +13 -0
- data/lib/sqb/version.rb +1 -1
- data/lib/sqb.rb +1 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62ef6126a194c6379a78a024567b91a2278d57cf
|
4
|
+
data.tar.gz: 74173493a093a294043c3f672079d64a58caa810
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9493b63fe326d3375ef2f4399ff623083da10e062e8697cf498d7c221793efa2919f601af7ea9ebf39ed282b98b24c72a1f82104a19b4f727c271324a0052d2
|
7
|
+
data.tar.gz: 1113bf3ae5beb73686c636382578bb7acb503c50bb143273417a353c3de3928ee280d973b3002733c9c5200dd088dce335a0c2566154505618dd1b87add02ea8
|
data/lib/sqb/base.rb
CHANGED
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
|
-
@
|
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
|
-
|
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
|
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 <<
|
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
|
-
@
|
25
|
+
if @record.nil?
|
26
|
+
record = (@local_record ||= {})
|
27
|
+
else
|
28
|
+
record = @record
|
29
|
+
end
|
30
|
+
|
25
31
|
hash.each do |key, value|
|
26
|
-
|
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
|
-
|
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
|
-
|
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
|
data/lib/sqb/replace.rb
ADDED
data/lib/sqb/version.rb
CHANGED
data/lib/sqb.rb
CHANGED
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.
|
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-
|
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
|