Sha256: 76e85cbcef1158662f8702819728a59f6deb1b78a31db3ce0d537e67bf129804

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module Scenic
  module Adapters
    class Postgres
      # Fetches defined views from the postgres connection.
      # @api private
      class Views
        def initialize(connection)
          @connection = connection
        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_postgres.map(&method(:to_scenic_view))
        end

        private

        attr_reader :connection

        def views_from_postgres
          connection.execute(<<-SQL)
            SELECT
              c.relname as viewname,
              pg_get_viewdef(c.oid) AS definition,
              c.relkind AS kind
            FROM pg_class c
              LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
            WHERE
              c.relkind IN ('m', 'v')
              AND c.relname NOT IN (SELECT extname FROM pg_extension)
              AND n.nspname = ANY (current_schemas(false))
            ORDER BY c.oid
          SQL
        end

        def to_scenic_view(result)
          Scenic::View.new(
            name: result["viewname"],
            definition: result["definition"].strip,
            materialized: result["kind"] == "m",
          )
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scenic-1.2.0 lib/scenic/adapters/postgres/views.rb
scenic-1.1.1 lib/scenic/adapters/postgres/views.rb
scenic-1.1.0 lib/scenic/adapters/postgres/views.rb