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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, records) ⇒ Result

:nodoc:



33
34
35
36
# File 'lib/simple/sql/result.rb', line 33

def initialize(connection, records) # :nodoc:
  @connection = connection
  replace(records)
end

Instance Attribute Details

#column_infoObject

Returns the value of attribute column_info.



7
8
9
# File 'lib/simple/sql/result.rb', line 7

def column_info
  @column_info
end

#connectionObject (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_pageObject

returns the current page number in a paginated search

This is only available for paginated scopes



56
57
58
# File 'lib/simple/sql/result.rb', line 56

def current_page
  @current_page ||= pagination_scope.page
end

#paginated?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/simple/sql/result.rb', line 60

def paginated?
  !!@pagination_scope
end

#pagination_scopeObject



64
65
66
67
68
# File 'lib/simple/sql/result.rb', line 64

def pagination_scope
  raise "Available only on paginated scopes" unless paginated?

  @pagination_scope
end

#pagination_scope=(scope) ⇒ Object

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/simple/sql/result.rb', line 70

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_countObject

returns the (potentialy slow) exact total count of results

This is only available for paginated scopes



48
49
50
51
# File 'lib/simple/sql/result.rb', line 48

def total_count
  # TODO: Implement total_count for non-paginated scopes!
  @total_count ||= pagination_scope.count
end

#total_count_estimateObject

returns the (potentialy estimated) total count of results

This is only available for paginated scopes



41
42
43
# File 'lib/simple/sql/result.rb', line 41

def total_count_estimate
  @total_count_estimate ||= pagination_scope.count_estimate
end