From c16dc42e96f2b0a8b93598733e284183ef309905 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Thu, 28 Jan 2016 20:51:03 -0300 Subject: [PATCH] allow `result_set#each` --- spec/std/db/dummy_driver_spec.cr | 11 +++++++++++ src/db/result_set.cr | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/spec/std/db/dummy_driver_spec.cr b/spec/std/db/dummy_driver_spec.cr index bb9a063..d8ff216 100644 --- a/spec/std/db/dummy_driver_spec.cr +++ b/spec/std/db/dummy_driver_spec.cr @@ -54,5 +54,16 @@ describe DummyDriver do result_set.read(UInt64).should eq(1) result_set.read(UInt64).should eq(2) end + + it "should enumerate records using each" do + nums = [] of UInt64 + result_set = get_dummy.prepare("3,4 1,2").exec + result_set.each do + nums << result_set.read(UInt64) + nums << result_set.read(UInt64) + end + + nums.should eq([3, 4, 1, 2]) + end end end diff --git a/src/db/result_set.cr b/src/db/result_set.cr index a9e10a1..147f8e3 100644 --- a/src/db/result_set.cr +++ b/src/db/result_set.cr @@ -5,6 +5,12 @@ module DB def initialize(@statement : Statement) end + def each + while has_next + yield + end + end + abstract def has_next : Bool # def read(t : T.class) : T