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
14 changes: 10 additions & 4 deletions lib/sqlite3/resultset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 28 additions & 2 deletions test/test_integration_resultset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
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.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
Expand Down