Method: RR::ConnectionExtenders::MysqlExtender#referenced_tables
- Defined in:
- lib/rubyrep/connection_extenders/mysql_extender.rb
#referenced_tables(tables) ⇒ Object
Returns for each given table, which other tables it references via foreign key constraints.
-
tables: an array of table names
Returns: a hash with
-
key: name of the referencing table
-
value: an array of names of referenced tables
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rubyrep/connection_extenders/mysql_extender.rb', line 36 def referenced_tables(tables) rows = self.select_all(" select distinct table_name as referencing_table, referenced_table_name as referenced_table\n from information_schema.key_column_usage\n where table_schema = database()\n and table_name in ('\#{tables.join(\"', '\")}')\n end_sql\n result = {}\n rows.each do |row|\n unless result.include? row['referencing_table']\n result[row['referencing_table']] = []\n end\n if row['referenced_table'] != nil\n result[row['referencing_table']] << row['referenced_table']\n end\n end\n tables.each do |table|\n result[table] = [] unless result.include? table\n end\n result\nend\n") |