simple-sql 0.4.4 → 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/Gemfile.lock +1 -1
- data/lib/simple/sql/scope.rb +37 -18
- data/lib/simple/sql/version.rb +1 -1
- data/spec/simple/sql_scope_spec.rb +16 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a83b1690cf4e3b75f7e974df31e7c8b29680e198
|
4
|
+
data.tar.gz: 333ca9858a0cc4eb06519d1eadee61fb875939f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce35aefb5d57370cb6fd88b1c25a5a9ebe3d767ce8bbb2e1b4433726e6968511ffa5a82859ae2b012b2f189b12761c20a266d7a21556f89303ac6f67c3ed4332
|
7
|
+
data.tar.gz: 812bc3709085da01e1fcd91a10f6c5c864a34f5a5fddb3edea5885ba22fcd0d5b273a8d6d300482d0a03c41ed14120c7c710aaa9e4e33a6489774d44782c3847
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
data/lib/simple/sql/scope.rb
CHANGED
@@ -39,37 +39,56 @@ class Simple::SQL::Scope
|
|
39
39
|
public
|
40
40
|
|
41
41
|
# scope = Scope.new("SELECT * FROM tablename")
|
42
|
+
# scope = scope.where(id: 12)
|
42
43
|
# scope = scope.where("id > ?", 12)
|
43
44
|
#
|
44
|
-
#
|
45
|
-
# argument (since postgres is using $1, $2, etc.)
|
46
|
-
# uses '?' as part of some fixed text you must
|
47
|
-
# placeholder symbol
|
45
|
+
# In the second form the placeholder (usually a '?') is being replaced
|
46
|
+
# with the numbered argument (since postgres is using $1, $2, etc.)
|
47
|
+
# If your SQL fragment uses '?' as part of some fixed text you must
|
48
|
+
# use an alternative placeholder symbol:
|
49
|
+
#
|
50
|
+
# scope = scope.where("foo | '?' = '^'", match, placeholder: '^')
|
48
51
|
#
|
49
|
-
# TODO: Add support for hash arguments, i.e.
|
50
|
-
# scope = scope.where(title: "foobar")
|
51
52
|
def where(sql_fragment, arg = :__dummy__no__arg, placeholder: "?")
|
52
53
|
duplicate.send(:where!, sql_fragment, arg, placeholder: placeholder)
|
53
54
|
end
|
54
55
|
|
55
|
-
def where!(
|
56
|
-
if arg
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
@filters << "#{key} = $#{@args.length}"
|
61
|
-
end
|
62
|
-
else
|
63
|
-
@filters << sql_fragment
|
64
|
-
end
|
56
|
+
def where!(first_arg, arg = :__dummy__no__arg, placeholder: "?")
|
57
|
+
if arg != :__dummy__no__arg
|
58
|
+
where_sql_with_argument!(first_arg, arg, placeholder: placeholder)
|
59
|
+
elsif first_arg.is_a?(Hash)
|
60
|
+
where_hash!(first_arg)
|
65
61
|
else
|
66
|
-
|
67
|
-
@filters << sql_fragment.gsub(placeholder, "$#{@args.length}")
|
62
|
+
where_sql!(first_arg)
|
68
63
|
end
|
69
64
|
|
70
65
|
self
|
71
66
|
end
|
72
67
|
|
68
|
+
def where_sql!(sql_fragment)
|
69
|
+
@filters << sql_fragment
|
70
|
+
end
|
71
|
+
|
72
|
+
def where_sql_with_argument!(sql_fragment, arg, placeholder:)
|
73
|
+
@args << arg
|
74
|
+
@filters << sql_fragment.gsub(placeholder, "$#{@args.length}")
|
75
|
+
end
|
76
|
+
|
77
|
+
def where_hash!(hsh)
|
78
|
+
hsh.each do |key, value|
|
79
|
+
raise ArgumentError, "condition key must be a Symbol or a String" unless key.is_a?(Symbol) || key.is_a?(String)
|
80
|
+
|
81
|
+
@args << value
|
82
|
+
|
83
|
+
case value
|
84
|
+
when Array
|
85
|
+
@filters << "#{key} = ANY($#{@args.length})"
|
86
|
+
else
|
87
|
+
@filters << "#{key} = $#{@args.length}"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
73
92
|
# Set pagination
|
74
93
|
def paginate(per:, page:)
|
75
94
|
duplicate.send(:paginate!, per: per, page: page)
|
data/lib/simple/sql/version.rb
CHANGED
@@ -48,6 +48,22 @@ describe "Simple::SQL::Scope" do
|
|
48
48
|
expect(SQL.ask(scope.where(id: user_id))).to eq(1)
|
49
49
|
end
|
50
50
|
end
|
51
|
+
|
52
|
+
context "with array arguments" do
|
53
|
+
it "matches against array arguments" do
|
54
|
+
expect(SQL.ask(scope.where("id" => [-333, user_id]))).to eq(1)
|
55
|
+
expect(SQL.ask(scope.where("id" => [-333, -1]))).to be_nil
|
56
|
+
expect(SQL.ask(scope.where("id" => []))).to be_nil
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "with invalid arguments" do
|
61
|
+
it "raises an ArgumentError" do
|
62
|
+
expect {
|
63
|
+
scope.where(1 => 3)
|
64
|
+
}.to raise_error(ArgumentError)
|
65
|
+
end
|
66
|
+
end
|
51
67
|
end
|
52
68
|
|
53
69
|
context "with non-argument conditions" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-sql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- radiospiel
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-05-
|
12
|
+
date: 2018-05-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg_array_parser
|