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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8dc4e5192271e99151c59877f89671f27f3333e6
4
- data.tar.gz: 986e1c338ed8c7c9571ea85797fd0f6444a8d8b3
3
+ metadata.gz: a83b1690cf4e3b75f7e974df31e7c8b29680e198
4
+ data.tar.gz: 333ca9858a0cc4eb06519d1eadee61fb875939f2
5
5
  SHA512:
6
- metadata.gz: 84e6f1f3e6bad60ca3611d103a84db49069132c07f3142a88764b8fd402daea2dff9b77ac2c1d2cd72eeddf4934b1441400486eeed430744423e65be014ca866
7
- data.tar.gz: 4d41a55a04b5f24cb0088fc4dd89badfdf510c7a2c06d8c64cafc3a50b3a414c8a01e66c75e245a179e527c561f5aa079f46454836cb1c5de88194ac0a7afd1c
6
+ metadata.gz: ce35aefb5d57370cb6fd88b1c25a5a9ebe3d767ce8bbb2e1b4433726e6968511ffa5a82859ae2b012b2f189b12761c20a266d7a21556f89303ac6f67c3ed4332
7
+ data.tar.gz: 812bc3709085da01e1fcd91a10f6c5c864a34f5a5fddb3edea5885ba22fcd0d5b273a8d6d300482d0a03c41ed14120c7c710aaa9e4e33a6489774d44782c3847
data/.rubocop.yml CHANGED
@@ -60,3 +60,6 @@ Style/RegexpLiteral:
60
60
 
61
61
  Style/ClassVars:
62
62
  Enabled: false
63
+
64
+ Style/ConditionalAssignment:
65
+ Enabled: false
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple-sql (0.4.4)
4
+ simple-sql (0.4.5)
5
5
  pg (~> 0.20)
6
6
  pg_array_parser (~> 0)
7
7
 
@@ -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
- # The placeholder (usually a '?') is being replaced with the numbered
45
- # argument (since postgres is using $1, $2, etc.) If your SQL fragment
46
- # uses '?' as part of some fixed text you must use an alternative
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!(sql_fragment, arg = :__dummy__no__arg, placeholder: "?")
56
- if arg == :__dummy__no__arg
57
- if sql_fragment.is_a?(Hash)
58
- sql_fragment.each do |key, value|
59
- @args << value
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
- @args << arg
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)
@@ -1,5 +1,5 @@
1
1
  module Simple
2
2
  module SQL
3
- VERSION = "0.4.4"
3
+ VERSION = "0.4.5"
4
4
  end
5
5
  end
@@ -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
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-29 00:00:00.000000000 Z
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