Class: Simple::SQL::Result
- Inherits:
-
Array
- Object
- Array
- Simple::SQL::Result
- Defined in:
- lib/simple/sql/result.rb,
lib/simple/sql/result.rb
Overview
The result of SQL.all
This class implements the basic interface of a Result set. Record result sets support the conversion of a record into a custom type of the callers choice, via the :into option for SQL.all and SQL.ask.
Direct Known Subclasses
Defined Under Namespace
Modules: AssociationLoader Classes: Records
Instance Attribute Summary collapse
-
#column_info ⇒ Object
Returns the value of attribute column_info.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Class Method Summary collapse
-
.build(connection, records, target_type:, pg_source_oid:) ⇒ Object
A Result object is requested via ::Simple::SQL::Result.build, which then chooses the correct implementation, based on the
target_type:parameter.
Instance Method Summary collapse
-
#current_page ⇒ Object
returns the current page number in a paginated search.
-
#initialize(connection, records) ⇒ Result
constructor
:nodoc:.
- #paginated? ⇒ Boolean
- #pagination_scope ⇒ Object
- #pagination_scope=(scope) ⇒ Object
-
#total_count ⇒ Object
returns the (potentialy slow) exact total count of results.
-
#total_count_estimate ⇒ Object
returns the (potentialy estimated) total count of results.
Constructor Details
#initialize(connection, records) ⇒ Result
:nodoc:
33 34 35 36 37 |
# File 'lib/simple/sql/result.rb', line 33 def initialize(connection, records) # :nodoc: super() @connection = connection replace(records) end |
Instance Attribute Details
#column_info ⇒ Object
Returns the value of attribute column_info.
7 8 9 |
# File 'lib/simple/sql/result.rb', line 7 def column_info @column_info end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
31 32 33 |
# File 'lib/simple/sql/result.rb', line 31 def connection @connection end |
Class Method Details
.build(connection, records, target_type:, pg_source_oid:) ⇒ Object
A Result object is requested via ::Simple::SQL::Result.build, which then chooses the correct implementation, based on the target_type: parameter.
23 24 25 26 27 28 29 |
# File 'lib/simple/sql/result.rb', line 23 def self.build(connection, records, target_type:, pg_source_oid:) # :nodoc: if target_type.nil? new(connection, records) else Records.new(connection, records, target_type: target_type, pg_source_oid: pg_source_oid) end end |
Instance Method Details
#current_page ⇒ Object
returns the current page number in a paginated search
This is only available for paginated scopes
57 58 59 |
# File 'lib/simple/sql/result.rb', line 57 def current_page @current_page ||= pagination_scope.page end |
#paginated? ⇒ Boolean
61 62 63 |
# File 'lib/simple/sql/result.rb', line 61 def paginated? !!@pagination_scope end |
#pagination_scope ⇒ Object
65 66 67 68 69 |
# File 'lib/simple/sql/result.rb', line 65 def pagination_scope raise "Available only on paginated scopes" unless paginated? @pagination_scope end |
#pagination_scope=(scope) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/simple/sql/result.rb', line 71 def pagination_scope=(scope) raise ArgumentError, "per must be > 0" unless scope.per > 0 @pagination_scope = scope # This branch is an optimization: the call to the database to count is # not necessary if we know that there are not even any results on the # first page. if scope.page <= 1 && empty? @current_page = 1 @total_count = 0 @total_count_estimate = 0 end end |
#total_count ⇒ Object
returns the (potentialy slow) exact total count of results
This is only available for paginated scopes
49 50 51 52 |
# File 'lib/simple/sql/result.rb', line 49 def total_count # TODO: Implement total_count for non-paginated scopes! @total_count ||= pagination_scope.count end |
#total_count_estimate ⇒ Object
returns the (potentialy estimated) total count of results
This is only available for paginated scopes
42 43 44 |
# File 'lib/simple/sql/result.rb', line 42 def total_count_estimate @total_count_estimate ||= pagination_scope.count_estimate end |