Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/sqlite3/resultset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def next
# Required by the Enumerable mixin. Provides an internal iterator over the
# rows of the result set.
def each
return enum_for(__method__) unless block_given?
while (node = self.next)
yield node
end
Expand All @@ -54,6 +55,7 @@ def each
# Provides an internal iterator over the rows of the result set where
# each row is yielded as a hash.
def each_hash
return enum_for(__method__) unless block_given?
while (node = next_hash)
yield node
end
Expand Down
9 changes: 8 additions & 1 deletion test/test_integration_resultset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ def test_each
assert_equal 2, called
end

def test_each_enum
called = 0
@result.reset(1, 2)
@result.each.to_a.each { |row| called += 1 }
assert_equal 2, called
end

def test_enumerable
@result.reset(1, 2)
assert_equal 2, @result.to_a.length
Expand All @@ -139,7 +146,7 @@ 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.close }
assert_raise(SQLite3::Exception) { result.types }
assert_raise(SQLite3::Exception) { result.columns }
Expand Down
6 changes: 6 additions & 0 deletions test/test_result_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 do |hash|
assert_equal list[hash["a"] - 1], hash["b"]
end
rs.close
end

def test_next_hash
Expand Down