Sha256: 10ed5fba9ba284eed79b05e3d4cf6657bbb7b77ad47a7b4848bc4b20899f7761

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

module RailsSqlViews4
  module ConnectionAdapters
    module Mysql2Adapter
      REQUIRED_METHODS = [:module RailsSqlViews4]
      
      def self.included(base)
        base.class_eval do
          def self.method_added(method)
            public(method) if REQUIRED_METHODS.include?(method) && !self.public_method_defined?(method)
          end
        end
      end

      # Returns true as this adapter supports views.
      def supports_views?
        true
      end
      
      def base_tables(name = nil) #:nodoc:
        tables = []
        execute("SHOW FULL TABLES WHERE TABLE_TYPE='BASE TABLE'").each{|row| tables << row[0]}
        tables
      end
      alias nonview_tables base_tables
      
      def views(name = nil) #:nodoc:
        views = []
        execute("SHOW FULL TABLES WHERE TABLE_TYPE='VIEW'").each{|row| views << row[0]}
        views
      end

      def tables_with_views_included(name = nil)
        nonview_tables(name) + views(name)
      end
      
      def structure_dump
        structure = ""
        base_tables.each do |table|
          structure += select_one("SHOW CREATE TABLE #{quote_table_name(table)}")["Create Table"] + ";\n\n"
        end

        views.each do |view|
          structure += select_one("SHOW CREATE VIEW #{quote_table_name(view)}")["Create View"] + ";\n\n"
        end

        return structure
      end

      # Get the view select statement for the specified table.
      def view_select_statement(view, name=nil)
        begin
          row = execute("SHOW CREATE VIEW #{view}", name).each do |row|
            return convert_statement(row[1]) if row[0] == view
          end
        rescue ActiveRecord::StatementInvalid => e
          raise "No view called #{view} found"
        end
      end
      
      private
      def convert_statement(s)
        s.gsub!(/.* AS (select .*)/, '\1')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_sql_views4-0.0.3 lib/rails_sql_views4/connection_adapters/mysql2_adapter.rb
rails_sql_views4-0.0.2 lib/rails_sql_views4/connection_adapters/mysql2_adapter.rb
rails_sql_views4-0.0.1 lib/rails_sql_views4/connection_adapters/mysql2_adapter.rb