Class: RDO::Result
Overview
The standard Result class returned by Connection#execute.
Both read and write queries receive results in this format.
Instance Method Summary collapse
-
#affected_rows ⇒ Fixnum
Return the number of rows affected by the query.
-
#count ⇒ Object
Get the number of rows in the result.
-
#each(&block) ⇒ Object
Iterate over all rows returned by the connection.
-
#empty? ⇒ Boolean
Check if the result has no rows.
-
#execution_time ⇒ Float
Get the time spent processing the statement.
-
#first_value ⇒ Object
If only one column and one row is expected in the result, fetch it.
-
#info ⇒ Hash
Get raw result info provided by the driver.
-
#initialize(tuples, info = {}) ⇒ Result
constructor
Initialize a new Result.
-
#insert_id ⇒ Object
Return the inserted row ID.
Constructor Details
#initialize(tuples, info = {}) ⇒ Result
Initialize a new Result.
26 27 28 29 |
# File 'lib/rdo/result.rb', line 26 def initialize(tuples, info = {}) @info = info.dup @tuples = tuples end |
Instance Method Details
#affected_rows ⇒ Fixnum
Return the number of rows affected by the query.
70 71 72 |
# File 'lib/rdo/result.rb', line 70 def affected_rows info[:affected_rows].to_i end |
#count ⇒ Object
Get the number of rows in the result.
Many drivers provide the count, otherwise it will be computed at runtime.
80 81 82 83 84 85 86 |
# File 'lib/rdo/result.rb', line 80 def count if info[:count].nil? || block_given? super else info[:count].to_i end end |
#each(&block) ⇒ Object
Iterate over all rows returned by the connection.
For each row, a Symbol-keyed Hash is yielded into the block.
107 108 109 |
# File 'lib/rdo/result.rb', line 107 def each(&block) tap{ @tuples.each(&block) } end |
#empty? ⇒ Boolean
Check if the result has no rows.
92 93 94 |
# File 'lib/rdo/result.rb', line 92 def empty? count.zero? end |
#execution_time ⇒ Float
Get the time spent processing the statement.
100 101 102 |
# File 'lib/rdo/result.rb', line 100 def execution_time info[:execution_time].to_f end |
#first_value ⇒ Object
If only one column and one row is expected in the result, fetch it.
If no rows were returned, this method returns nil.
60 61 62 63 64 |
# File 'lib/rdo/result.rb', line 60 def first_value if row = first row.values.first end end |
#info ⇒ Hash
Get raw result info provided by the driver.
35 36 37 |
# File 'lib/rdo/result.rb', line 35 def info @info end |
#insert_id ⇒ Object
Return the inserted row ID.
For some drivers this requires that a RETURNING clause by used in SQL. It may be more desirable to simply check the rows in the result.
46 47 48 49 50 51 52 |
# File 'lib/rdo/result.rb', line 46 def insert_id if info.key?(:insert_id) info[:insert_id] else first_value end end |