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
# 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|
    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.



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

def drop_view(view_name, options = {})
  SchemaMonkey::Middleware::Migration::DropView.start(connection: self, view_name: view_name, options: options) do |env|
    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

#view_definition(view_name, name = nil) ⇒ Object

(abstract) 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.



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

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

#views(name = nil) ⇒ Object

(abstract) Returns the names of all views, as an array of strings



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

def views(name = nil) raise "Internal Error: Connection adapter didn't override abstract function"; [] end