Class: Ladybug::Result
- Inherits:
-
Object
- Object
- Ladybug::Result
- Extended by:
- Loggability
- Defined in:
- lib/ladybug/result.rb
Overview
Kùzu query result class
These objects contain one result set from either a Ladybug::Connection#query call or Ladybug::PreparedStatement#execute. If there are multiple result sets, you can fetch the next one by calling Ladybug::Result#next_set. You can use #has_next_set? to test for a following set.
Tuple values are converted to corresponding Ruby objects:
| Ladybug Type | Ruby Type |
|---|---|
INT8 |
Integer |
INT16 |
Integer |
INT32 |
Integer |
INT64 |
Integer |
INT128 |
Integer |
UINT8 |
Integer |
UINT16 |
Integer |
UINT32 |
Integer |
UINT64 |
Integer |
FLOAT |
Float |
DOUBLE |
Float |
DECIMAL |
Float |
BOOLEAN |
TrueClass or FalseClass |
UUID |
String (UTF-8 encoding) |
STRING |
String (UTF-8 encoding) |
NULL |
NilClass |
DATE |
Date |
TIMESTAMP |
Time |
INTERVAL |
Float (interval in seconds) |
STRUCT |
OpenStruct via the ostruct standard library |
MAP |
Hash |
UNION |
(not yet handled) |
BLOB |
String (+ASCII_8BIT+ encoding) |
SERIAL |
Integer |
NODE |
Ladybug::Node |
REL |
Ladybug::Rel |
RECURSIVE_REL |
Ladybug::RecursiveRel |
LIST |
Array |
ARRAY |
Array |
Class Method Summary collapse
-
.from_prepared_statement(statement, &block) ⇒ Object
Execute the given
statementand return the Ladybug::Result. -
.from_query(connection, query, &block) ⇒ Object
Execute the given
queryvia the specifiedconnectionand return the Ladybug::Result. -
.wrap_block_result(result, &block) ⇒ Object
If the
blockis provided, yieldresultto it and then call #finish on it, returning theblockresult.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Index operator: fetch the tuple at
indexof the current result set. -
#column_names ⇒ Object
Fetch the names of the columns in the result as an Array of Strings.
-
#each(&block) ⇒ Object
Iterate over each tuple of the result, yielding it to the
block. -
#each_set(&block) ⇒ Object
Iterate over each result set in the results, yielding it to the block.
-
#inspect ⇒ Object
Return a string representation of the receiver suitable for debugging.
-
#next ⇒ Object
Get the next tuple of the result as a Hash.
-
#next_set ⇒ Object
Return the next result set after this one as a Ladybug::Result, or
nilif there is no next set. -
#query_summary ⇒ Object
Return a Ladybug::QuerySummary for the query that generated the Result.
-
#tuples ⇒ Object
Return the tuples from the current result set.
Class Method Details
.from_prepared_statement(statement, &block) ⇒ Object
Execute the given statement and return the Ladybug::Result. If a block is given,
the result will instead be yielded to it, finished when it returns, and the
return value of the block will be returned instead.
70 71 72 |
# File 'lib/ladybug/result.rb', line 70 def self::from_prepared_statement( statement, &block ) return statement.execute( &block ) end |
.from_query(connection, query, &block) ⇒ Object
Execute the given query via the specified connection and return the
Ladybug::Result. If a block is given, the result will instead be yielded to it,
finished when it returns, and the return value of the block will be returned
instead.
62 63 64 |
# File 'lib/ladybug/result.rb', line 62 def self::from_query( connection, query, &block ) return connection.query( query, &block ) end |
.wrap_block_result(result, &block) ⇒ Object
If the block is provided, yield result to it and then call #finish on it,
returning the block result. If block is not given, just return result.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ladybug/result.rb', line 77 def self::wrap_block_result( result, &block ) return result unless block begin rval = block.call( result ) ensure result.finish end return rval end |
Instance Method Details
#[](index) ⇒ Object
Index operator: fetch the tuple at index of the current result set.
127 128 129 |
# File 'lib/ladybug/result.rb', line 127 def []( index ) return self.tuples[ index ] end |
#column_names ⇒ Object
Fetch the names of the columns in the result as an Array of Strings.
91 92 93 |
# File 'lib/ladybug/result.rb', line 91 def column_names return @column_names ||= self.get_column_names end |
#each(&block) ⇒ Object
Iterate over each tuple of the result, yielding it to the block. If no
block is given, return an Enumerator that will yield them instead.
112 113 114 115 116 |
# File 'lib/ladybug/result.rb', line 112 def each( &block ) enum = self.tuple_enum return enum.each( &block ) if block return enum end |
#each_set(&block) ⇒ Object
Iterate over each result set in the results, yielding it to the block. If
no block is given, return an Enumerator tht will yield each set as its own
Result.
143 144 145 146 147 |
# File 'lib/ladybug/result.rb', line 143 def each_set( &block ) enum = self.next_set_enum return enum.each( &block ) if block return enum end |
#inspect ⇒ Object
Return a string representation of the receiver suitable for debugging.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/ladybug/result.rb', line 151 def inspect if self.finished? details = " (finished)" else details = " success: %p (%d tuples of %d columns)" % [ self.success?, self.num_tuples, self.num_columns, ] end default = super return default.sub( />/, details + '>' ) end |
#next ⇒ Object
Get the next tuple of the result as a Hash.
103 104 105 106 107 |
# File 'lib/ladybug/result.rb', line 103 def next values = self.get_next_values or return nil pairs = self.column_names.zip( values ) return Hash[ pairs ] end |
#next_set ⇒ Object
Return the next result set after this one as a Ladybug::Result, or nilif
there is no next set.
134 135 136 137 |
# File 'lib/ladybug/result.rb', line 134 def next_set return nil unless self.has_next_set? return self.class.from_next_set( self ) end |
#query_summary ⇒ Object
Return a Ladybug::QuerySummary for the query that generated the Result.
97 98 99 |
# File 'lib/ladybug/result.rb', line 97 def query_summary return Ladybug::QuerySummary.from_result( self ) end |
#tuples ⇒ Object
Return the tuples from the current result set. This method is memoized for efficiency.
121 122 123 |
# File 'lib/ladybug/result.rb', line 121 def tuples return @_tuples ||= self.to_a end |