Sha256: fed2f206927ada866fb72319c4f89f693acbbd956915bded326dd63d126b1f87

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module Scenic
  module Adapters
    class Mysql

      # Fetches defined views from the mysql connection.
      # @api private
      class Views
        def initialize(connection)
          @connection = connection
        end

        def current_database
          @connection.current_database
        end

        # All of the views that this connection has defined.
        #
        # This will include materialized views if those are supported by the
        # connection.
        #
        # @return [Array<Scenic::View>]
        def all
          views_from_mysql.map(&method(:to_scenic_view))
        end

        private

        attr_reader :connection

        def views_from_mysql
          connection.select_rows(<<-SQL)
            SELECT table_name, view_definition
            FROM information_schema.views
            WHERE table_schema = SCHEMA()
          SQL
        end

        def to_scenic_view(result)
          table_name, view_def = result

          Scenic::View.new(
            name: table_name,
            definition: scrub_view_def(view_def)
          )
        end

        # Sometimes MySQL will include the database name
        # in view definitions.  This scrubs it out.
        #
        def scrub_view_def(view_def)
          view_def.strip.gsub(/([\`]*#{current_database}[\`]*\.)/i, '')
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scenic_mysql-0.1.2 lib/scenic/adapters/mysql/views.rb