Class: ActiveRecord::ConnectionAdapters::PostgreSQLMaterializedViewDefinition

Inherits:
Object
  • Object
show all
Includes:
PostgreSQLExtensions::Utils
Defined in:
lib/active_record/postgresql_extensions/materialized_views.rb

Overview

Creates a PostgreSQL materialized view definition. This class isn’t really meant to be used directly. Instead, see PostgreSQLAdapter#create_materialized_view for usage.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PostgreSQLExtensions::Utils

#hash_or_array_of_hashes, #options_from_hash_or_string, #strip_heredoc

Constructor Details

#initialize(base, name, query, options = {}) ⇒ PostgreSQLMaterializedViewDefinition

:nodoc:



163
164
165
# File 'lib/active_record/postgresql_extensions/materialized_views.rb', line 163

def initialize(base, name, query, options = {}) #:nodoc:
  @base, @name, @query, @options = base, name, query, options
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



161
162
163
# File 'lib/active_record/postgresql_extensions/materialized_views.rb', line 161

def base
  @base
end

#nameObject

Returns the value of attribute name.



161
162
163
# File 'lib/active_record/postgresql_extensions/materialized_views.rb', line 161

def name
  @name
end

#optionsObject

Returns the value of attribute options.



161
162
163
# File 'lib/active_record/postgresql_extensions/materialized_views.rb', line 161

def options
  @options
end

#queryObject

Returns the value of attribute query.



161
162
163
# File 'lib/active_record/postgresql_extensions/materialized_views.rb', line 161

def query
  @query
end

Instance Method Details

#to_sqlObject Also known as: to_s

:nodoc:



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/active_record/postgresql_extensions/materialized_views.rb', line 167

def to_sql #:nodoc:
  sql = "CREATE MATERIALIZED VIEW #{base.quote_view_name(name)} "

  if options[:columns]
    sql << '(' << Array.wrap(options[:columns]).collect do |c|
      base.quote_column_name(c)
    end.join(', ') << ') '
  end

  sql << "WITH (#{options_from_hash_or_string(options[:with_options])}) " if options[:with_options].present?

  sql << "TABLESPACE #{base.quote_tablespace(options[:tablespace])} " if options[:tablespace]
  sql << "AS #{query}"
  sql << " WITH NO DATA" if options.key?(:with_data) && !options[:with_data]
  "#{sql};"
end