Module: SchemaPlus::Views::ActiveRecord::ConnectionAdapters::AbstractAdapter

Defined in:
lib/schema_plus/views/active_record/connection_adapters/abstract_adapter.rb

Instance Method Summary collapse

Instance Method Details

#create_view(view_name, definition, options = {}) ⇒ Object

Create a view given the SQL definition. Specify :force => true to first drop the view if it already exists.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/schema_plus/views/active_record/connection_adapters/abstract_adapter.rb', line 7

def create_view(view_name, definition, options={})
  SchemaMonkey::Middleware::Migration::CreateView.start(connection: self, view_name: view_name, definition: definition, options: options) do |env|
    raise ArgumentError, 'Materialized views are not implemented or supported on this database' if options[:materialized]
    definition = env.definition
    view_name = env.view_name
    options = env.options
    definition = definition.to_sql if definition.respond_to? :to_sql
    if options[:force]
      drop_view(view_name, if_exists: true)
    end

    command = if options[:allow_replace]
                "CREATE OR REPLACE"
              else
                "CREATE"
              end

    execute "#{command} VIEW #{quote_table_name(view_name)} AS #{definition}"
  end
end

#drop_view(view_name, options = {}) ⇒ Object

Drop the named view. Specify :if_exists => true to fail silently if the view doesn’t exist.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/schema_plus/views/active_record/connection_adapters/abstract_adapter.rb', line 30

def drop_view(view_name, options = {})
  SchemaMonkey::Middleware::Migration::DropView.start(connection: self, view_name: view_name, options: options) do |env|
    raise ArgumentError, 'Materialized views are not implemented or supported on this database' if options[:materialized]
    view_name = env.view_name
    options = env.options
    sql = "DROP VIEW"
    sql += " IF EXISTS" if options[:if_exists]
    sql += " #{quote_table_name(view_name)}"
    execute sql
  end
end

#refresh_view(view_name, name = nil) ⇒ Object

(abstract) Refreshes the given materialized view.



61
# File 'lib/schema_plus/views/active_record/connection_adapters/abstract_adapter.rb', line 61

def refresh_view(view_name, name = nil) raise "Internal Error: Connection adapter didn't override abstract function"; end

#view_definition(view_name, name = nil) ⇒ Object

Returns the SQL definition of a given view. This is the literal SQL would come after ‘CREATVE VIEW viewname AS ’ in the SQL statement to create a view.



45
46
47
# File 'lib/schema_plus/views/active_record/connection_adapters/abstract_adapter.rb', line 45

def view_definition(view_name, name = nil)
  view_full_definition(view_name, name).first
end

#view_full_definition(view_name, name = nil) ⇒ Object

(abstract) Returns the SQL definition and type of a given view. This is the literal SQL would come after ‘CREATVE VIEW viewname AS ’ in the SQL statement to create a view. The type is either :view, or :materialized



66
# File 'lib/schema_plus/views/active_record/connection_adapters/abstract_adapter.rb', line 66

def view_full_definition(view_name, name = nil) raise "Internal Error: Connection adapter didn't override abstract function"; end

#view_type(view_name, name = nil) ⇒ Object

Returns the view type of a given view. This is either :view or :materialized



50
51
52
# File 'lib/schema_plus/views/active_record/connection_adapters/abstract_adapter.rb', line 50

def view_type(view_name, name = nil)
  view_full_definition(view_name, name).second
end