Sha256: 9841a0379a6fa674effa8d268c881b79c09456cd2793815b47dc8402c68eb665

Contents?: true

Size: 1.98 KB

Versions: 12

Compression:

Stored size: 1.98 KB

Contents

module RR

  module ConnectionExtenders

    # Provides various MySQL specific functionality required by Rubyrep.
    module MysqlExtender
      RR::ConnectionExtenders.register :mysql => self

      # Returns an ordered list of primary key column names of the given table
      def primary_key_names(table)
        row = self.select_one(<<-end_sql)
          select table_name from information_schema.tables 
          where table_schema = database() and table_name = '#{table}'
        end_sql
        if row.nil?
          raise "table '#{table}' does not exist"
        end
        
        rows = self.select_all(<<-end_sql)
          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
        end_sql

        columns = rows.map {|_row| _row['column_name']}
        columns
      end

      # 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
      def referenced_tables(tables)
        rows = self.select_all(<<-end_sql)
          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("', '")}')
        end_sql
        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
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rubyrep-1.2.0 lib/rubyrep/connection_extenders/mysql_extender.rb
rubyrep-1.1.2 lib/rubyrep/connection_extenders/mysql_extender.rb
rubyrep-1.1.1 lib/rubyrep/connection_extenders/mysql_extender.rb
rubyrep-1.1.0 lib/rubyrep/connection_extenders/mysql_extender.rb
rubyrep-1.0.9 lib/rubyrep/connection_extenders/mysql_extender.rb
rubyrep-1.0.8 lib/rubyrep/connection_extenders/mysql_extender.rb
rubyrep-1.0.3 lib/rubyrep/connection_extenders/mysql_extender.rb
rubyrep-1.0.4 lib/rubyrep/connection_extenders/mysql_extender.rb
rubyrep-1.0.5 lib/rubyrep/connection_extenders/mysql_extender.rb
rubyrep-1.0.6 lib/rubyrep/connection_extenders/mysql_extender.rb
rubyrep-1.0.7 lib/rubyrep/connection_extenders/mysql_extender.rb
rubyrep-1.0.2 lib/rubyrep/connection_extenders/mysql_extender.rb