Skip to content

Commit 4c5259a

Browse files
committed
add any(), all(), haskey()
1 parent 6edce15 commit 4c5259a

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/dictionary.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Base.keys(d::SQLDictionary) = map(_keyoptic(d), d.coll)
2424
Dictionaries.issettable(::SQLDictionary) = true
2525
Dictionaries.isinsertable(::SQLDictionary) = true
2626

27+
Base.haskey(d::SQLDictionary, i) = any((@o _keyoptic(d)(_) == i), d.coll)
28+
2729
function Base.getindex(d::SQLDictionary, i)
2830
vals = @p filter((@o _keyoptic(d)(_) == i), d.coll) map(_valoptic(d)) first(__, 2) collect
2931
@assert length(vals) 1 "Didn't expect multiple values for key $i, got $(length(vals))"

src/readfuncs.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Base.only(dbc::SQLCollection) = first(dbc, 2) |> collect |> only
4343

4444
Base.isempty(dbc::SQLCollection) = first(dbc, 1) |> collect |> isempty
4545

46+
Base.any(pred, dbc::SQLCollection) = !(@p dbc filter(pred) isempty)
47+
Base.all(pred, dbc::SQLCollection) = @p dbc filter(!pred) isempty
48+
4649
Base.length(dbc::SQLCollection) = count(Returns(true), dbc)
4750

4851
Base.count(pred, dbc::SQLCollection) = @modify(dbc.query) do q

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ using TestItemRunner
149149
length,
150150
(@f count(Returns(true))),
151151
(@f count(@o _.i > 7)),
152+
(@f any(Returns(true))),
153+
(@f any(@o _.i > 7)),
154+
(@f any(@o _.i > 100)),
155+
(@f all(Returns(true))),
156+
(@f all(@o _.i > 7)),
157+
(@f all(@o _.i > 100)),
152158
(@f sort(by=(@o (_.i, -_.j)), rev=true) Iterators.drop(__, 5) first),
153159
(@f sort(by=(@o (_.i, -_.j)), rev=true) Iterators.drop(__, 5) first(__, 1) only),
154160
(@f mean(@o _.i)),
@@ -261,11 +267,19 @@ end
261267
@test first(dct) == (x=1.1, y="def")
262268

263269
@test dct[(a=1, b="a")] == (x=1.1, y="def")
270+
@test dct[(b="a", a=1)] == (x=1.1, y="def")
264271
@test_throws KeyError((a=1, b="c")) dct[(a=1, b="c")]
265272
@test_throws KeyError((a=1, b="c")) dct[(a=1, b="c")] = (x=1.3, y="ghi")
273+
@test_throws AssertionError dct[123]
274+
@test_throws AssertionError dct[(a=1, b="c", c="d")]
266275
dct[(a=1, b="a")] = (x=1.3, y="ghi")
267276
@test dct[(a=1, b="a")] == (x=1.3, y="ghi")
268277

278+
@test haskey(dct, (a=1, b="a"))
279+
@test !haskey(dct, (a=1, b="c"))
280+
@test_throws AssertionError haskey(dct, (a=1, b="c", c="d"))
281+
@test_throws AssertionError haskey(dct, 123)
282+
269283
delete!(dct, (a=1, b="a"))
270284
@test_throws KeyError((a=1, b="a")) delete!(dct, (a=1, b="a"))
271285
@test collect(dct) == [(x=1.2, y="xyz"), (x=2.1, y="abc")]

0 commit comments

Comments
 (0)