Class: ActiveRecord::ConnectionAdapters::Mysql2Adapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/connection_manager/patches/cross_schema_patch.rb

Instance Method Summary collapse

Instance Method Details

#cached_tablesObject

Force all tables to be cached for the life connection



38
39
40
# File 'lib/connection_manager/patches/cross_schema_patch.rb', line 38

def cached_tables
  @cached_tables ||= {}
end

#table_exists?(name) ⇒ Boolean

We have to clean the name of ‘`’ and fetch table name with schema

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
# File 'lib/connection_manager/patches/cross_schema_patch.rb', line 54

def table_exists?(name)
  return false unless name
  name          = name.to_s
  schema, table = name.split('.', 2)
  unless table # A table was provided without a schema
    table  = schema
    schema = nil
  end
  tables(nil, schema, table).include?(table)
end

#tables(name = nil, database = nil, like = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/connection_manager/patches/cross_schema_patch.rb', line 42

def tables(name = nil, database = nil, like =nil)
  return cached_tables[database] if cached_tables[database] && like.nil?
  cached_tables[database] ||= []
  return [like] if like && cached_tables[database].include?(like)
  sql = "SHOW TABLES "
  sql << "IN #{database} " if database
  sql << "LIKE #{quote(like)}" if like
  result = execute(sql, 'SCHEMA')
  cached_tables[database] = (cached_tables[database] | result.collect { |field| field[0] }).compact
end