predicate 2.9.0 → 2.11.0
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/lib/predicate/nodes/or.rb +32 -0
- data/lib/predicate/sequel/to_sequel.rb +6 -1
- data/lib/predicate/version.rb +1 -1
- data/spec/predicate/test_to_hash.rb +18 -0
- data/spec/predicate/test_to_hashes.rb +78 -0
- data/spec/sequel/test_to_sequel.rb +38 -15
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6fd6ea9f498fb935b7445d0486d1ff4387da5f8e9d71525c762abeb214353b13
|
|
4
|
+
data.tar.gz: f0bbf5080ea295803a10823984d5d99112033a2ea98dbaf1bdc3846fadd65db0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4422e7cf2eda59e119df2e1aa89fc703a9dc935c1bd80a9d625d6281316786fb2e56d5ad6c4dcd2a06a77a58510df9f74b2a3cf0e735a8f5b4f6014487a6ed4e
|
|
7
|
+
data.tar.gz: 310883c2b4d6c7e8661219969097e199a7bd9f6a6cd3ef520c93026a1b42485cf52b492449320d833aa0e00ff398d17cae00f7cd2ec089edbfb3a5a90eeebf52
|
data/lib/predicate/nodes/or.rb
CHANGED
|
@@ -10,5 +10,37 @@ class Predicate
|
|
|
10
10
|
sexpr_body.any?{|op| op.evaluate(tuple) }
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
+
# A disjunction can be represented as a `{ attr => [values] }` hash only
|
|
14
|
+
# when every operand constrains the *same single* attribute to a literal
|
|
15
|
+
# or a set of literals, e.g. `x == 1 OR x == 2 OR x IN [3, 4]`. Operands
|
|
16
|
+
# are folded together into an IN-style hash. As soon as an operand touches
|
|
17
|
+
# another attribute, or cannot itself be represented as a hash, the whole
|
|
18
|
+
# disjunction is not representable and an ArgumentError is raised (as for
|
|
19
|
+
# any other non-representable predicate).
|
|
20
|
+
def to_hash
|
|
21
|
+
merged = sexpr_body.inject(nil) do |acc, term|
|
|
22
|
+
hash = term.to_hash
|
|
23
|
+
return super unless hash.size == 1
|
|
24
|
+
|
|
25
|
+
attr, value = hash.first
|
|
26
|
+
values = value.is_a?(Array) ? value : [value]
|
|
27
|
+
|
|
28
|
+
if acc.nil?
|
|
29
|
+
[attr, values.dup]
|
|
30
|
+
elsif acc.first == attr
|
|
31
|
+
[attr, acc.last + values]
|
|
32
|
+
else
|
|
33
|
+
return super
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
return super if merged.nil?
|
|
38
|
+
{ merged.first => merged.last.uniq }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def to_hashes
|
|
42
|
+
[ to_hash, {} ]
|
|
43
|
+
end
|
|
44
|
+
|
|
13
45
|
end
|
|
14
46
|
end
|
|
@@ -110,11 +110,16 @@ class Predicate
|
|
|
110
110
|
raise Error, "Unable to compile #{sexpr} to Sequel"
|
|
111
111
|
end
|
|
112
112
|
|
|
113
|
+
def on_native(sexpr)
|
|
114
|
+
return sexpr.last.sql_literal if sexpr.last.respond_to?(:sql_literal)
|
|
115
|
+
return sexpr.last.sql if sexpr.last.respond_to?(:sql)
|
|
116
|
+
on_unsupported(sexpr)
|
|
117
|
+
end
|
|
118
|
+
|
|
113
119
|
def on_unsupported(sexpr)
|
|
114
120
|
raise NotSupportedError, "Unsupported predicate #{sexpr}"
|
|
115
121
|
end
|
|
116
122
|
alias :on_var :on_unsupported
|
|
117
|
-
alias :on_native :on_unsupported
|
|
118
123
|
alias :on_intersect :on_unsupported
|
|
119
124
|
alias :on_subset :on_unsupported
|
|
120
125
|
alias :on_superset :on_unsupported
|
data/lib/predicate/version.rb
CHANGED
|
@@ -36,5 +36,23 @@ class Predicate
|
|
|
36
36
|
it{ expect(subject).to eql(x: 3, y: [2,3]) }
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
+
context "or of eqs on the same attribute" do
|
|
40
|
+
let(:predicate){ Predicate.eq(:x, 2) | Predicate.eq(:x, 3) }
|
|
41
|
+
|
|
42
|
+
it{ expect(subject).to eql(x: [2,3]) }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
context "or mixing eq and in on the same attribute" do
|
|
46
|
+
let(:predicate){ Predicate.eq(:x, 2) | Predicate.in(:x, [3,4]) }
|
|
47
|
+
|
|
48
|
+
it{ expect(subject).to eql(x: [2,3,4]) }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
context "or on distinct attributes" do
|
|
52
|
+
let(:predicate){ Predicate.eq(:x, 2) | Predicate.eq(:y, 3) }
|
|
53
|
+
|
|
54
|
+
it{ expect{ subject }.to raise_error(ArgumentError) }
|
|
55
|
+
end
|
|
56
|
+
|
|
39
57
|
end
|
|
40
58
|
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
class Predicate
|
|
3
|
+
describe Predicate, "to_hashes" do
|
|
4
|
+
|
|
5
|
+
let(:p){ Predicate }
|
|
6
|
+
|
|
7
|
+
subject{ predicate.to_hashes }
|
|
8
|
+
|
|
9
|
+
context "tautology" do
|
|
10
|
+
let(:predicate){ Predicate.tautology }
|
|
11
|
+
|
|
12
|
+
it{ expect(subject).to eql([{},{}]) }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
context "contradiction" do
|
|
16
|
+
let(:predicate){ Predicate.contradiction }
|
|
17
|
+
|
|
18
|
+
it{ expect{ subject }.to raise_error(ArgumentError) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
context "eq" do
|
|
22
|
+
let(:predicate){ Predicate.eq(:x, 2) }
|
|
23
|
+
|
|
24
|
+
it{ expect(subject).to eql([{x: 2}, {}]) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
context "neq" do
|
|
28
|
+
let(:predicate){ Predicate.neq(:x, 2) }
|
|
29
|
+
|
|
30
|
+
it{ expect(subject).to eql([{},{x: 2}]) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
context "in" do
|
|
34
|
+
let(:predicate){ Predicate.in(:x, [2,3]) }
|
|
35
|
+
|
|
36
|
+
it{ expect(subject).to eql([{x: [2,3]},{}]) }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
context "and" do
|
|
40
|
+
let(:predicate){ Predicate.eq(:x, 3) & Predicate.in(:y, [2,3]) }
|
|
41
|
+
|
|
42
|
+
it{ expect(subject).to eql([{x: 3, y: [2,3]},{}]) }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
context "and with a neq" do
|
|
46
|
+
let(:predicate){ Predicate.neq(:x, 3) & Predicate.in(:y, [2,3]) }
|
|
47
|
+
|
|
48
|
+
it{ expect(subject).to eql([{y: [2,3]},{x: 3}]) }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
context "or of eqs on the same attribute" do
|
|
52
|
+
let(:predicate){ Predicate.eq(:x, 2) | Predicate.eq(:x, 3) }
|
|
53
|
+
|
|
54
|
+
it{ expect(subject).to eql([{x: [2,3]},{}]) }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
context "not nil & eq" do
|
|
58
|
+
let(:predicate){ Predicate.neq(:x, nil) & Predicate.eq(:x, [2,3]) }
|
|
59
|
+
|
|
60
|
+
it{ expect(subject).to eql([{x: [2,3]},{x: nil}]) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
## unsupported
|
|
64
|
+
|
|
65
|
+
context "not" do
|
|
66
|
+
let(:predicate){ Predicate.not(Predicate.eq(:x, 2)) }
|
|
67
|
+
|
|
68
|
+
it{ expect{ subject }.to raise_error(ArgumentError) }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
context "not-and" do
|
|
72
|
+
let(:predicate){ Predicate.not(Predicate.eq(:x, 3) & Predicate.in(:y, [2,3])) }
|
|
73
|
+
|
|
74
|
+
it{ expect{ subject }.to raise_error(ArgumentError) }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -101,21 +101,6 @@ class Predicate
|
|
|
101
101
|
end
|
|
102
102
|
end
|
|
103
103
|
|
|
104
|
-
context 'in with something responding to sql_literal' do
|
|
105
|
-
let(:operand){
|
|
106
|
-
Object.new.tap{|o|
|
|
107
|
-
def o.sql_literal(db)
|
|
108
|
-
"Hello World"
|
|
109
|
-
end
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
let(:predicate) { Predicate.in(:price, Predicate.opaque(operand)) }
|
|
113
|
-
|
|
114
|
-
it 'works as expected' do
|
|
115
|
-
expect(subject).to eql("SELECT * FROM `items` WHERE (`price` IN (Hello World))")
|
|
116
|
-
end
|
|
117
|
-
end
|
|
118
|
-
|
|
119
104
|
context 'not' do
|
|
120
105
|
let(:predicate) { !Predicate.in(:price, [10.0, 17.99]) }
|
|
121
106
|
|
|
@@ -220,5 +205,43 @@ class Predicate
|
|
|
220
205
|
expect { subject }.to raise_error(NotSupportedError)
|
|
221
206
|
end
|
|
222
207
|
end
|
|
208
|
+
|
|
209
|
+
class SqlAble
|
|
210
|
+
def initialize(value)
|
|
211
|
+
@value = value
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def sql(*args, &bl)
|
|
215
|
+
@value
|
|
216
|
+
end
|
|
217
|
+
alias :sql_literal :sql
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
context 'opaque in an IN' do
|
|
221
|
+
let(:operand) {
|
|
222
|
+
SqlAble.new('Hello World')
|
|
223
|
+
}
|
|
224
|
+
let(:predicate) {
|
|
225
|
+
Predicate.in(:price, Predicate.opaque(operand))
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
it 'works as expected' do
|
|
229
|
+
expect(subject).to eql("SELECT * FROM `items` WHERE (`price` IN (Hello World))")
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
context 'native' do
|
|
234
|
+
let(:operand) {
|
|
235
|
+
SqlAble.new(Sequel.lit("1=1"))
|
|
236
|
+
}
|
|
237
|
+
let(:predicate) {
|
|
238
|
+
Predicate.native(operand)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
it 'works as expected' do
|
|
242
|
+
expect(subject).to eql("SELECT * FROM `items` WHERE (1=1)")
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
|
|
223
246
|
end
|
|
224
247
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: predicate
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.11.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bernard Lambeau
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-07-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: sexpr
|
|
@@ -251,6 +251,7 @@ files:
|
|
|
251
251
|
- spec/predicate/test_rename.rb
|
|
252
252
|
- spec/predicate/test_tautology.rb
|
|
253
253
|
- spec/predicate/test_to_hash.rb
|
|
254
|
+
- spec/predicate/test_to_hashes.rb
|
|
254
255
|
- spec/predicate/test_to_s.rb
|
|
255
256
|
- spec/predicate/test_unqualify.rb
|
|
256
257
|
- spec/sequel/test_to_sequel.rb
|