mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Add #query_each . Fixes #18
This commit is contained in:
parent
5697bd5c58
commit
39e17f82ca
2 changed files with 36 additions and 4 deletions
|
@ -208,6 +208,24 @@ describe DummyDriver do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "query each" do
|
||||||
|
it "queries" do
|
||||||
|
with_dummy do |db|
|
||||||
|
i = 0
|
||||||
|
db.query_each "3,4 1,2" do |rs|
|
||||||
|
case i
|
||||||
|
when 0
|
||||||
|
rs.read(Int64, Int64).should eq({3i64, 4i64})
|
||||||
|
when 1
|
||||||
|
rs.read(Int64, Int64).should eq({1i64, 2i64})
|
||||||
|
end
|
||||||
|
i += 1
|
||||||
|
end
|
||||||
|
i.should eq(2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "reads multiple values" do
|
it "reads multiple values" do
|
||||||
with_dummy do |db|
|
with_dummy do |db|
|
||||||
db.query "3,4 1,2" do |rs|
|
db.query "3,4 1,2" do |rs|
|
||||||
|
|
|
@ -166,10 +166,8 @@ module DB
|
||||||
# ```
|
# ```
|
||||||
def query_all(query, *args, &block : ResultSet -> U) : Array(U) forall U
|
def query_all(query, *args, &block : ResultSet -> U) : Array(U) forall U
|
||||||
ary = [] of U
|
ary = [] of U
|
||||||
query(query, *args) do |rs|
|
query_each(query, *args) do |rs|
|
||||||
rs.each do
|
ary.push(yield rs)
|
||||||
ary.push(yield rs)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
ary
|
ary
|
||||||
end
|
end
|
||||||
|
@ -198,6 +196,22 @@ module DB
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Executes a *query* and yields the `ResultSet` once per each row.
|
||||||
|
# The `ResultSet` is closed automatically.
|
||||||
|
#
|
||||||
|
# ```
|
||||||
|
# db.query_each "select name from contacts" do |rs|
|
||||||
|
# puts rs.read(String)
|
||||||
|
# end
|
||||||
|
# ```
|
||||||
|
def query_each(query, *args)
|
||||||
|
query(query, *args) do |rs|
|
||||||
|
rs.each do
|
||||||
|
yield rs
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Performs the `query` and returns an `ExecResult`
|
# Performs the `query` and returns an `ExecResult`
|
||||||
def exec(query, *args)
|
def exec(query, *args)
|
||||||
build(query).exec(*args)
|
build(query).exec(*args)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue