Class: Simple::SQL::Result

Inherits:
Array
  • Object
show all
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

Records

Defined Under Namespace

Modules: AssociationLoader Classes: Records

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records) ⇒ Result

:nodoc:



29
30
31
# File 'lib/simple/sql/result.rb', line 29

def initialize(records) # :nodoc:
  replace(records)
end

Class Method Details

.build(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.



21
22
23
24
25
26
27
# File 'lib/simple/sql/result.rb', line 21

def self.build(records, target_type:, pg_source_oid:) # :nodoc:
  if target_type.nil?
    new(records)
  else
    Records.new(records, target_type: target_type, pg_source_oid: pg_source_oid)
  end
end

Instance Method Details

#current_pageObject

returns the current page number in a paginated search

This is filled in when resolving a paginated scope.



78
79
80
# File 'lib/simple/sql/result.rb', line 78

def current_page
  @current_page ||= @pagination_scope.page
end

#total_countObject

returns the total_count of search hits

This is filled in when resolving a paginated scope.



59
60
61
62
63
64
65
# File 'lib/simple/sql/result.rb', line 59

def total_count
  @total_count ||= begin
    scope = @pagination_scope
    scope_sql = scope.order_by(nil).to_sql(pagination: false)
    ::Simple::SQL.ask("SELECT COUNT(*) FROM (#{scope_sql}) simple_sql_count", *scope.args)
  end
end

#total_count_estimateObject

returns a fast estimate for the total_count of search hits

This is filled in when resolving a paginated scope.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/simple/sql/result.rb', line 36

def total_count_estimate
  @total_count_estimate ||= catch(:total_count_estimate) do
    scope = @pagination_scope
    scope_sql = scope.order_by(nil).to_sql(pagination: false)
    ::Simple::SQL.each("EXPLAIN #{scope_sql}", *scope.args) do |line|
      next unless line =~ /\brows=(\d+)/

      throw :total_count_estimate, Integer($1)
    end
    -1
  end
end

#total_pagesObject

returns the total number of pages of search hits

This is filled in when resolving a paginated scope. It takes into account the scope’s “per” option.



71
72
73
# File 'lib/simple/sql/result.rb', line 71

def total_pages
  @total_pages ||= (total_count * 1.0 / @pagination_scope.per).ceil
end

#total_pages_estimateObject

returns the estimated total number of pages of search hits

This is filled in when resolving a paginated scope.



52
53
54
# File 'lib/simple/sql/result.rb', line 52

def total_pages_estimate
  @total_pages_estimate ||= (total_count_estimate * 1.0 / @pagination_scope.per).ceil
end