Module: ActiveRecord::Relation::QueryMethodsExt

Defined in:
lib/active_record/relation/query_methods_ext.rb

Instance Method Summary collapse

Instance Method Details

#aost(time) ⇒ Object

Set system time for the current query. Using ‘.aost(nil)` resets.

See cockroachlabs.com/docs/stable/as-of-system-time



33
34
35
# File 'lib/active_record/relation/query_methods_ext.rb', line 33

def aost(time)
  spawn.aost!(time)
end

#aost!(time) ⇒ Object

:nodoc:



20
21
22
23
24
25
26
27
# File 'lib/active_record/relation/query_methods_ext.rb', line 20

def aost!(time) # :nodoc:
  unless time.nil? || time.is_a?(Time)
    raise ArgumentError, "Unsupported argument type: #{time} (#{time.class})"
  end

  @aost = time
  self
end

#force_index(index_name, direction: nil) ⇒ Object

Set table index hint for the query to the given ‘index_name`, and `direction` (either `ASC` or `DESC`).

Any call to ‘ActiveRecord::QueryMethods#from` will reset the index hint. Index hints are not set if the `from` clause is not a table name.



53
54
55
# File 'lib/active_record/relation/query_methods_ext.rb', line 53

def force_index(index_name, direction: nil)
  spawn.force_index!(index_name, direction: direction)
end

#force_index!(index_name, direction: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_record/relation/query_methods_ext.rb', line 57

def force_index!(index_name, direction: nil)
  return self unless from_clause_is_a_table_name?

  index_name = model.sanitize_sql(index_name.to_s)
  direction = direction.to_s.upcase
  direction = %w[ASC DESC].include?(direction) ? ",#{direction}" : ""

  @force_index = "FORCE_INDEX=#{index_name}#{direction}"
  self.from_clause = build_from_clause_with_hints
  self
end

#from!Object

:nodoc:



37
38
39
40
41
# File 'lib/active_record/relation/query_methods_ext.rb', line 37

def from!(...) # :nodoc:
  @force_index = nil
  @index_hint = nil
  super
end

#index_hint(hint) ⇒ Object

Set table index hint for the query with the given ‘hint`. This allows more control over the hint than `ActiveRecord::Relation#force_index`. For instance, you could set it to `NO_FULL_SCAN`.

Any call to ‘ActiveRecord::QueryMethods#from` will reset the index hint. Index hints are not set if the `from` clause is not a table name.



80
81
82
# File 'lib/active_record/relation/query_methods_ext.rb', line 80

def index_hint(hint)
  spawn.index_hint!(hint)
end

#index_hint!(hint) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/active_record/relation/query_methods_ext.rb', line 84

def index_hint!(hint)
  return self unless from_clause_is_a_table_name?

  hint = model.sanitize_sql(hint.to_s)
  @index_hint = hint.to_s
  self.from_clause = build_from_clause_with_hints
  self
end

#show_createObject



93
94
95
96
# File 'lib/active_record/relation/query_methods_ext.rb', line 93

def show_create
  quoted_table = connection.quote_table_name self.table_name
  connection.select_one("show create table #{quoted_table}")["create_statement"]
end