Module: SimpleOracleJDBC::ResultSet

Included in:
Sql
Defined in:
lib/simple_oracle_jdbc/result_set.rb

Overview

This module provides a wrapper around a JDBC result set object, allowing the result set to be returned a row at a time, as an array or arrays or an array of hashes etc.

Requires a instance variable called @result_set to be defined in the class this module is included into. This should contain a JDBC result set object.

Instance Method Summary collapse

Instance Method Details

#all_arrayObject

Consumes all the rows of the result set, and returns the result as an array of arrays.

An empty array will be returned if the result set contains no rows.

If the result set contains a lot of rows, this will create an array that requires a lot of memory, so use with caution.

The result set will be closed when the method returns.



40
41
42
# File 'lib/simple_oracle_jdbc/result_set.rb', line 40

def all_array
  all_results(true)
end

#all_hashObject

Consumes all the rows of the result set, and returns the result as an array of hashes.

An empty array will be returned if the result set contains no rows.

If the result set contains a lot of rows, this will create an array that requires a lot of memory, so use with caution.

The result set will be closed when the method returns.



71
72
73
# File 'lib/simple_oracle_jdbc/result_set.rb', line 71

def all_hash
  all_results(false)
end

#close_result_setObject

Closes the result set if it exists, and also closes the SQL statement that created the result set. TODO - does it make sense to close the statement here too?



78
79
80
81
82
83
84
# File 'lib/simple_oracle_jdbc/result_set.rb', line 78

def close_result_set
  if @result_set
    @result_set.close
    @result_set = nil
  end
  close_statement
end

#each_array(&blk) ⇒ Object

Processes each row of the result set using the provided block, passing one row at a time to the block as an array, eg:

obj.each_array do |r|
  # process the row
end

The result set will be closed when the method returns.



28
29
30
# File 'lib/simple_oracle_jdbc/result_set.rb', line 28

def each_array(&blk)
  each_result true, &blk
end

#each_hash(&blk) ⇒ Object

Processes each row of the result set using hte provided block, passing one row as a time to the block as a hash, eg:

obj.each_hash do |r|
  # process the row
end

The result set will be closed when the method returns.



59
60
61
# File 'lib/simple_oracle_jdbc/result_set.rb', line 59

def each_hash(&blk)
  each_result false, &blk
end

#next_arrayObject

Retrieves the next row from the database and returns an array. Each element in the array corresponds to a column in the select statement that created the result set.

The value nil will be returned if there are no more rows to return, and the result set will be closed.



16
17
18
# File 'lib/simple_oracle_jdbc/result_set.rb', line 16

def next_array
  next_row
end

#next_hashObject

Similar to next_array, only it returns the result as a hash. The hash will have one key for each column in the corresponding select statement. The key will be the upper case name of the column or alias from the SQL statement.



47
48
49
# File 'lib/simple_oracle_jdbc/result_set.rb', line 47

def next_hash
  next_row(false)
end