Skip to content

Commit

Permalink
ResultSetEnumerator#each to return Enumerator when block not given
Browse files Browse the repository at this point in the history
  • Loading branch information
junegunn committed Jun 17, 2013
1 parent 9310fca commit 902e696
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
* Added MariaDB connector
* Added SQLite connector
* Deprecated the use of `JDBCHelper::SQL`
* `ResultSetEnumerator#each` returns an enumerator when block is not given

### 0.7.7 / 2013/01/0?
* `PreparedStatment`s and `TableWrapper`s now inherit the fetch size of the connection
Expand Down
4 changes: 2 additions & 2 deletions lib/jdbc-helper/connection.rb
Expand Up @@ -315,10 +315,10 @@ def query(qstr, &blk)
# @yield [JDBCHelper::Connection::Row] Yields each record if block is given
# @return [JDBCHelper::Connection::ResultSetEnumerator] Returns an enumerator if block is not given
def enumerate(qstr, &blk)
check_closed

return query(qstr, &blk) if block_given?

check_closed

stmt = @spool.take
begin
rset = stmt.execute_query(qstr)
Expand Down
1 change: 1 addition & 0 deletions lib/jdbc-helper/connection/result_set_enumerator.rb
Expand Up @@ -11,6 +11,7 @@ class ResultSetEnumerator
include Enumerable

def each
return enum_for(:each) unless block_given?
return if closed?

count = -1
Expand Down
29 changes: 29 additions & 0 deletions test/test_connection.rb
Expand Up @@ -210,6 +210,35 @@ def test_query_enumerate
assert_equal 2, a.length
check_one_two a.first
assert enum.closed? == true

# Enumerator chain
cnt = 0
enum = conn.enumerate(get_one_two)
assert_equal false, enum.closed?
enum.each.each.each.each_slice(1) do |slice|
assert_equal Array, slice.class
assert_equal 1, slice.length
cnt += 1
assert_equal false, enum.closed?
end
assert_equal true, enum.closed?
assert_equal 2, cnt

cnt = 0
enum = conn.enumerate(get_one_two)
assert_equal false, enum.closed?
enum.each.each.each.with_index.each_slice(2).each do |slice|
assert_equal Array, slice.class
assert_equal 2, slice.length
assert_equal Array, slice[0].class
assert_equal 2, slice[0].length
assert_equal cnt, slice[0][1]
assert_equal cnt + 1, slice[1][1]
cnt += 2
assert_equal false, enum.closed?
end
assert_equal true, enum.closed?
assert_equal 2, cnt
end
end

Expand Down

0 comments on commit 902e696

Please sign in to comment.