Module: ConnectionManager::AbstractAdapterHelper

Defined in:
lib/connection_manager/helpers/abstract_adapter_helper.rb

Instance Method Summary collapse

Instance Method Details

#configObject



3
4
5
# File 'lib/connection_manager/helpers/abstract_adapter_helper.rb', line 3

def config
  @config
end

#cross_database_support?Boolean Also known as: cross_schema_support?

Determines if connection supports cross database queries

Returns:

  • (Boolean)


8
9
10
# File 'lib/connection_manager/helpers/abstract_adapter_helper.rb', line 8

def cross_database_support?
  (@config[:cross_database_support] || @config[:adapter].match(/(mysql)|(postgres)|(sqlserver)/i))
end

#database_nameObject



25
26
27
# File 'lib/connection_manager/helpers/abstract_adapter_helper.rb', line 25

def database_name
  @config[:database]
end

#fetch_full_table_name(table_name) ⇒ Object

Returns table_schema.table_name for the given table. Returns nil if multiple matches are found



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/connection_manager/helpers/abstract_adapter_helper.rb', line 59

def fetch_full_table_name(table_name)
  return table_name if (table_name.to_s.match(/(^$)|(\.)/))
  sql = "SELECT CONCAT(table_schema,'.',table_name) FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '#{table_name}'"
  found = nil
  results = execute(sql, 'SCHEMA')
  found = results.to_a
  if (found.length == 1)
    found = found[0][0]
  else
    found = table_name
  end
  found
end

#fetch_table_schema(table_name) ⇒ Object

Returns the schema for a give table. Returns nil of multiple matches are found



47
48
49
50
51
52
53
54
55
56
# File 'lib/connection_manager/helpers/abstract_adapter_helper.rb', line 47

def fetch_table_schema(table_name)
  sql = "SELECT table_schema FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '#{table_name}'"
  found = nil
  results = execute(sql, 'SCHEMA')
  found = results.to_a
  if (found.length == 1)
    found = found[0][0]
  end
  found
end

#master_keysObject



39
40
41
42
# File 'lib/connection_manager/helpers/abstract_adapter_helper.rb', line 39

def master_keys
  return @config[:masters].collect{|r| r.to_sym} if @config[:masters]
  []
end

#readonly?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/connection_manager/helpers/abstract_adapter_helper.rb', line 17

def readonly?
  (@config[:readonly] == true)
end

#replicated?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/connection_manager/helpers/abstract_adapter_helper.rb', line 21

def replicated?
  (!slave_keys.blank? || !master_keys.blank?)
end

#replication_keys(type = :slaves) ⇒ Object



29
30
31
32
# File 'lib/connection_manager/helpers/abstract_adapter_helper.rb', line 29

def replication_keys(type=:slaves)
  return slave_keys if type == :slaves
  master_keys
end

#slave_keysObject



34
35
36
37
# File 'lib/connection_manager/helpers/abstract_adapter_helper.rb', line 34

def slave_keys
  return @config[:slaves].collect{|r| r.to_sym} if @config[:slaves]
  []
end

#using_em_adapter?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/connection_manager/helpers/abstract_adapter_helper.rb', line 13

def using_em_adapter?
  (@config[:adapter].match(/^em\_/) && defined?(EM) && EM::reactor_running?)
end