Module: RailsSqlViews::ConnectionAdapters::SchemaStatements

Included in:
ActiveRecord::ConnectionAdapters::AbstractAdapter
Defined in:
lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#create_view(name, select_query, options = {}) ⇒ Object

Create a view. The options hash can include the following keys:

:check_option

Specify restrictions for inserts or updates in updatable views. ANSI SQL 92 defines two check option values: CASCADED and LOCAL. See your database documentation for allowed values.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb', line 9

def create_view(name, select_query, options={})
  if supports_views?
    view_definition = ViewDefinition.new(self, select_query)

    yield view_definition

    if options[:force]
      drop_view(name) rescue nil
    end

    create_sql = "CREATE VIEW "
    create_sql << "#{name} ("
    create_sql << view_definition.to_sql
    create_sql << ") AS #{view_definition.select_query}"
    create_sql << " WITH #{options[:check_option]} CHECK OPTION" if options[:check_option]
    execute create_sql
  end
end

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

Drop a view. The options hash can include the following keys:

:drop_behavior

Specify the drop behavior. ANSI SQL 92 defines two drop behaviors, CASCADE and RESTRICT. See your database documentation to determine what drop behaviors are available.



33
34
35
36
37
38
39
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb', line 33

def drop_view(name, options={})
  if supports_views?
    drop_sql = "DROP VIEW #{name}"
    drop_sql << " #{options[:drop_behavior]}" if options[:drop_behavior]
    execute drop_sql
  end
end