sorted 2.1.2 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/sorted/parse.rb +31 -2
- data/lib/sorted/sql_query.rb +27 -2
- data/lib/sorted/uri_query.rb +4 -1
- data/lib/sorted/version.rb +1 -1
- data/spec/sql_query_spec.rb +30 -0
- data/spec/uri_query_spec.rb +30 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38dd0916f6d792b758402c94839d2277b863dcf9
|
4
|
+
data.tar.gz: 669640e3a8adf55ad839b0218f0082319c4b8e24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f243889b5495aa2cf02a368da79aef5a98e1e3b7d7faef66ef7dabe09ca1b012535ade8f2db7674dae9ea37ad8bcb57f91510028197f48c0e0fa288118b90b0
|
7
|
+
data.tar.gz: 4d38c93addb9797e764f91a2d3bf47794f4f7117d214280d78cb5ff4a1db376a6928f33ebf833dfc78a85579c9accf67c555c1df42418e5f86bc172a17067e54
|
data/README.md
CHANGED
data/lib/sorted/parse.rb
CHANGED
@@ -7,8 +7,37 @@ module Sorted
|
|
7
7
|
raw.to_s.split(delim).inject(Set.new, &block)
|
8
8
|
end
|
9
9
|
|
10
|
-
def parse_match(
|
11
|
-
|
10
|
+
def parse_match(raw)
|
11
|
+
return parse_sort_with_direction(raw) if raw[5].nil?
|
12
|
+
|
13
|
+
parse_sort_without_direction(raw)
|
14
|
+
end
|
15
|
+
|
16
|
+
def parse_sort_with_direction(raw)
|
17
|
+
parsed = []
|
18
|
+
parsed << parse_column(raw[1], raw[2])
|
19
|
+
parsed << parse_direction(raw[3])
|
20
|
+
parsed << parse_nulls(raw[4]) unless raw[4].nil?
|
21
|
+
parsed
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse_sort_without_direction(raw)
|
25
|
+
parsed = []
|
26
|
+
parsed << parse_column(raw[1], raw[5])
|
27
|
+
parsed << parse_nulls(raw[6]) unless raw[6].nil?
|
28
|
+
parsed
|
29
|
+
end
|
30
|
+
|
31
|
+
def parse_column(raw_all_content, raw_column)
|
32
|
+
raw_column || raw_all_content
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_direction(raw_direction)
|
36
|
+
raw_direction.nil? ? 'asc' : raw_direction.downcase
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_nulls(raw_nulls)
|
40
|
+
raw_nulls.downcase
|
12
41
|
end
|
13
42
|
end
|
14
43
|
end
|
data/lib/sorted/sql_query.rb
CHANGED
@@ -5,7 +5,11 @@ module Sorted
|
|
5
5
|
class SQLQuery
|
6
6
|
extend Parse
|
7
7
|
|
8
|
-
REGEXP =
|
8
|
+
REGEXP = %r{
|
9
|
+
(([a-z0-9._]+)\s([asc|desc]+)\s?(nulls\s[first|last]+)?|
|
10
|
+
([a-z0-9._]+)\s(nulls\s[first|last]+)|
|
11
|
+
([a-z0-9._]+))
|
12
|
+
}xi
|
9
13
|
|
10
14
|
def self.parse(raw)
|
11
15
|
split(raw, /,/) do |set, part|
|
@@ -16,8 +20,29 @@ module Sorted
|
|
16
20
|
end
|
17
21
|
|
18
22
|
def self.encode(set, quote_proc = ->(f) { f })
|
19
|
-
set.map
|
23
|
+
set.map do |a|
|
24
|
+
encoded = "#{column(a[0], quote_proc)}"
|
25
|
+
|
26
|
+
if sort_has_direction?(a)
|
27
|
+
encoded += " #{a[1].upcase}"
|
28
|
+
encoded += encode_nulls(a[2]) unless a[2].nil?
|
29
|
+
else
|
30
|
+
encoded += encode_nulls(a[1]) unless a[1].nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
encoded
|
34
|
+
end.join(', ')
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.sort_has_direction?(raw)
|
38
|
+
raw[1].match(/ASC|DESC/i) unless raw[1].nil?
|
39
|
+
end
|
40
|
+
private_class_method :sort_has_direction?
|
41
|
+
|
42
|
+
def self.encode_nulls(raw_nulls)
|
43
|
+
" #{raw_nulls.upcase.tr('_', ' ')}"
|
20
44
|
end
|
45
|
+
private_class_method :encode_nulls
|
21
46
|
|
22
47
|
def self.column(parts, quote_proc)
|
23
48
|
parts.split('.').map { |frag| quote_proc.call(frag) }.join('.')
|
data/lib/sorted/uri_query.rb
CHANGED
@@ -5,7 +5,10 @@ module Sorted
|
|
5
5
|
class URIQuery
|
6
6
|
extend Parse
|
7
7
|
|
8
|
-
REGEXP =
|
8
|
+
REGEXP = %r{
|
9
|
+
(([a-z0-9._]+)_([asc|desc]+)_?(nulls_first|nulls_last)?|
|
10
|
+
([a-z0-9._]+)_(nulls_first|nulls_last))
|
11
|
+
}xi
|
9
12
|
|
10
13
|
def self.parse(raw)
|
11
14
|
split(raw, /!/) do |set, part|
|
data/lib/sorted/version.rb
CHANGED
data/spec/sql_query_spec.rb
CHANGED
@@ -34,6 +34,22 @@ describe Sorted::SQLQuery, 'decode' do
|
|
34
34
|
|
35
35
|
expect(set).to eq(result)
|
36
36
|
end
|
37
|
+
|
38
|
+
it 'should decode nulls first/last' do
|
39
|
+
sql = 'email ASC NULLS FIRST, phone ASC, name DESC NULLS LAST'
|
40
|
+
set = Sorted::SQLQuery.parse(sql)
|
41
|
+
result = Sorted::Set.new([['email', 'asc', 'nulls first'], ['phone', 'asc'], ['name', 'desc', 'nulls last']])
|
42
|
+
|
43
|
+
expect(set).to eq(result)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should decode nulls first/last without asc/desc' do
|
47
|
+
sql = 'email NULLS FIRST, phone, name NULLS LAST'
|
48
|
+
set = Sorted::SQLQuery.parse(sql)
|
49
|
+
result = Sorted::Set.new([['email', 'nulls first'], ['phone', 'asc'], ['name', 'nulls last']])
|
50
|
+
|
51
|
+
expect(set).to eq(result)
|
52
|
+
end
|
37
53
|
end
|
38
54
|
|
39
55
|
describe Sorted::SQLQuery, 'encode' do
|
@@ -58,4 +74,18 @@ describe Sorted::SQLQuery, 'encode' do
|
|
58
74
|
|
59
75
|
expect(Sorted::SQLQuery.encode(set, quoter)).to eq(result)
|
60
76
|
end
|
77
|
+
|
78
|
+
it 'should encode nulls first/last' do
|
79
|
+
set = Sorted::Set.new([['email', 'desc', 'nulls_last'], ['name', 'desc'], ['phone', 'asc', 'nulls_first']])
|
80
|
+
result = '`email` DESC NULLS LAST, `name` DESC, `phone` ASC NULLS FIRST'
|
81
|
+
|
82
|
+
expect(Sorted::SQLQuery.encode(set, quoter)).to eq(result)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should encode nulls first/last without asc/desc' do
|
86
|
+
set = Sorted::Set.new([['email', 'nulls_last'], ['name'], ['phone', 'nulls_first']])
|
87
|
+
result = '`email` NULLS LAST, `name`, `phone` NULLS FIRST'
|
88
|
+
|
89
|
+
expect(Sorted::SQLQuery.encode(set, quoter)).to eq(result)
|
90
|
+
end
|
61
91
|
end
|
data/spec/uri_query_spec.rb
CHANGED
@@ -10,6 +10,22 @@ describe Sorted::URIQuery, 'decode' do
|
|
10
10
|
|
11
11
|
expect(set).to eq(result)
|
12
12
|
end
|
13
|
+
|
14
|
+
it 'should decode nulls first/last' do
|
15
|
+
uri = 'table_name.column_name_desc_nulls_last!table_name.column_name_5_asc_nulls_first'
|
16
|
+
set = Sorted::URIQuery.parse(uri)
|
17
|
+
result = [['table_name.column_name', 'desc', 'nulls_last'], ['table_name.column_name_5', 'asc', 'nulls_first']]
|
18
|
+
|
19
|
+
expect(set).to eq(result)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should decode nulls first/last without asc/desc' do
|
23
|
+
uri = 'table_name.column_name_nulls_last!table_name.column_name_5_nulls_first'
|
24
|
+
set = Sorted::URIQuery.parse(uri)
|
25
|
+
result = [['table_name.column_name', 'nulls_last'], ['table_name.column_name_5', 'nulls_first']]
|
26
|
+
|
27
|
+
expect(set).to eq(result)
|
28
|
+
end
|
13
29
|
end
|
14
30
|
|
15
31
|
describe Sorted::URIQuery, 'encode' do
|
@@ -19,4 +35,18 @@ describe Sorted::URIQuery, 'encode' do
|
|
19
35
|
|
20
36
|
expect(Sorted::URIQuery.encode(set)).to eq(result)
|
21
37
|
end
|
38
|
+
|
39
|
+
it 'should encode nulls first/last' do
|
40
|
+
set = Sorted::Set.new([%w[column_name desc nulls_last], %w[column_name_5 asc nulls_first]])
|
41
|
+
result = 'column_name_desc_nulls_last!column_name_5_asc_nulls_first'
|
42
|
+
|
43
|
+
expect(Sorted::URIQuery.encode(set)).to eq(result)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should encode nulls first/last' do
|
47
|
+
set = Sorted::Set.new([%w[column_name nulls_last], %w[column_name_5 nulls_first]])
|
48
|
+
result = 'column_name_nulls_last!column_name_5_nulls_first'
|
49
|
+
|
50
|
+
expect(Sorted::URIQuery.encode(set)).to eq(result)
|
51
|
+
end
|
22
52
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sorted
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rufus Post
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: 1.3.6
|
120
120
|
requirements: []
|
121
121
|
rubyforge_project: sorted
|
122
|
-
rubygems_version: 2.
|
122
|
+
rubygems_version: 2.5.2.3
|
123
123
|
signing_key:
|
124
124
|
specification_version: 4
|
125
125
|
summary: Data sorting library
|