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
LOCK_FUNCTIONS =

Regexp fragments, not literal names: PostgreSQL's advisory lock family has too many members to list.

%w(
  nextval currval lastval setval get_lock release_lock is_free_lock is_used_lock
  pg_(?:try_)?advisory_(?:xact_)?(?:un)?lock(?:_shared|_all)?
).freeze
ROW_LOCK_STRENGTHS =

A string rather than a Regexp: interpolating a Regexp below would wrap it in a group that resets the /im flags, so FOR UPDATE would stop matching.

'no\s+key\s+update|key\s+share|update|share'
SQL_PRIMARY_MATCHERS =
[
  /\A\s*select\b.*\bfor\s+(?:#{ROW_LOCK_STRENGTHS})\b/im,
  /\A\s*select\b.*\block\s+in\s+share\s+mode\b/im,
  /\A\s*select\b.*\b(#{LOCK_FUNCTIONS.join('|')})\s*\(/im,
  /\A\s*show\b/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



33
34
35
36
# File 'lib/janus-ar/query_director.rb', line 33

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

Instance Method Details

#where_to_send?Boolean



38
39
40
41
42
43
44
45
46
# File 'lib/janus-ar/query_director.rb', line 38

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