diff --git a/lib/sqlite3/resultset.rb b/lib/sqlite3/resultset.rb index 8af61913..e8d7b2b9 100644 --- a/lib/sqlite3/resultset.rb +++ b/lib/sqlite3/resultset.rb @@ -43,20 +43,26 @@ def next @stmt.step end - # Required by the Enumerable mixin. Provides an internal iterator over the - # rows of the result set. + # With a block given, iterates over the rows of the result set, passing each + # to the block. Returns self. + # With no block given, returns a new Enumerator. def each + return enum_for(__method__) unless block_given? while (node = self.next) yield node end + self end - # Provides an internal iterator over the rows of the result set where - # each row is yielded as a hash. + # With a block given, iterates over the rows of the result set, passing each + # to the block as a hash. Returns self. + # With no block given, returns a new Enumerator. def each_hash + return enum_for(__method__) unless block_given? while (node = next_hash) yield node end + self end # Closes the statement that spawned this result set. diff --git a/test/test_integration_resultset.rb b/test/test_integration_resultset.rb index 531d6154..9c7c312c 100644 --- a/test/test_integration_resultset.rb +++ b/test/test_integration_resultset.rb @@ -113,10 +113,35 @@ def test_next_results_as_hash def test_each called = 0 @result.reset(1, 2) - @result.each { |row| called += 1 } + result = @result.each { |row| called += 1 } + result.reset # reset just to confirm we can chain the method after each + assert_equal @result, result assert_equal 2, called end + def test_each_enum + @result.reset(1, 2) + enum = @result.each + assert_instance_of Enumerator, enum + assert_equal 2, enum.to_a.length + end + + def test_each_hash + called = 0 + @result.reset(1, 2) + result = @result.each_hash { |row| called += 1 } + result.reset + assert_equal @result, result + assert_equal 2, called + end + + def test_each_hash_enum + @result.reset(1, 2) + enum = @result.each_hash + assert_instance_of Enumerator, enum + assert_equal 2, enum.to_a.length + end + def test_enumerable @result.reset(1, 2) assert_equal 2, @result.to_a.length @@ -139,7 +164,8 @@ def test_close assert_predicate stmt, :closed? assert_raise(SQLite3::Exception) { result.reset } assert_raise(SQLite3::Exception) { result.next } - assert_raise(SQLite3::Exception) { result.each } + assert_raise(SQLite3::Exception) { result.each.next } + assert_raise(SQLite3::Exception) { result.each_hash.next } assert_raise(SQLite3::Exception) { result.close } assert_raise(SQLite3::Exception) { result.types } assert_raise(SQLite3::Exception) { result.columns } diff --git a/test/test_result_set.rb b/test/test_result_set.rb index 4fc12280..02b85691 100644 --- a/test/test_result_set.rb +++ b/test/test_result_set.rb @@ -24,6 +24,12 @@ def test_each_hash assert_equal list[hash["a"] - 1], hash["b"] end rs.close + + rs = @db.prepare("select * from foo").execute + rs.each_hash.to_a.each do |hash| # each_hash without block, to_a confirms enum + assert_equal list[hash["a"] - 1], hash["b"] + end + rs.close end def test_next_hash