Class: Janus::QueryDirector

Inherits:
Object
  • Object
show all
Defined in:
lib/janus-ar/query_director.rb

Constant Summary collapse

ALL =
:all
REPLICA =
:replica
PRIMARY =
:primary
SQL_PRIMARY_MATCHERS =
[
  /\A\s*select.+for update\Z/i, /select.+lock in share mode\Z/i,
  /\A\s*select.+(nextval|currval|lastval|get_lock|release_lock|pg_advisory_lock|pg_advisory_unlock)\(/i,
  /\A\s*show/i
].freeze
SQL_REPLICA_MATCHERS =
[/\A\s*(select|with.+\)\s*select)\s/i].freeze
SQL_ALL_MATCHERS =
[/\A\s*set\s/i].freeze
SQL_SKIP_ALL_MATCHERS =
[/\A\s*set\s+local\s/i].freeze
LEADING_NOISE =

Leading whitespace and SQL comments are stripped before matching so that an annotated statement (e.g. ‘/* app:web */ INSERT …`) is classified by the statement itself rather than by the comment.

%r{\A(?:\s+|/\*.*?\*/|--[^\n]*(?:\n|\z)|\#[^\n]*(?:\n|\z))+}m

Instance Method Summary collapse

Constructor Details

#initialize(sql, open_transactions) ⇒ QueryDirector

Returns a new instance of QueryDirector.



22
23
24
25
# File 'lib/janus-ar/query_director.rb', line 22

def initialize(sql, open_transactions)
  @_sql = sql
  @_open_transactions = open_transactions
end

Instance Method Details

#where_to_send?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
# File 'lib/janus-ar/query_director.rb', line 27

def where_to_send?
  if should_send_to_all?
    ALL
  elsif can_go_to_replica?
    REPLICA
  else
    PRIMARY
  end
end