module RR::ConnectionExtenders::MysqlExtender

Provides various MySQL specific functionality required by Rubyrep.

Public Instance Methods

primary_key_names(table) click to toggle source

Returns an ordered list of primary key column names of the given table

# File lib/rubyrep/connection_extenders/mysql_extender.rb, line 10
      def primary_key_names(table)
        row = self.select_one("          select table_name from information_schema.tables 
          where table_schema = database() and table_name = '#{table}'
")
        if row.nil?
          raise "table '#{table}' does not exist"
        end
        
        rows = self.select_all("          select column_name from information_schema.key_column_usage
          where table_schema = database() and table_name = '#{table}' 
          and constraint_name = 'PRIMARY'
          order by ordinal_position
")

        columns = rows.map {|_row| _row['column_name']}
        columns
      end
referenced_tables(tables) click to toggle source

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

# 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
          from information_schema.key_column_usage
          where table_schema = database()
          and table_name in ('#{tables.join("', '")}')
")
        result = {}
        rows.each do |row|
          unless result.include? row['referencing_table']
            result[row['referencing_table']] = []
          end
          if row['referenced_table'] != nil
            result[row['referencing_table']] << row['referenced_table']
          end
        end
        tables.each do |table|
          result[table] = [] unless result.include? table
        end
        result
      end