Module: RailsSqlViews::ConnectionAdapters::SchemaStatements

Defined in:
lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb', line 4

def self.included(base)
  base.alias_method_chain :drop_table, :cascade
end

Instance Method Details

#create_mapping_view(old_name, new_name, options = {}) {|mapper| ... } ⇒ Object

Also creates a view, with the specific purpose of remapping column names to make non-ActiveRecord tables friendly with the naming conventions, while maintaining legacy app compatibility.

Yields:

  • (mapper)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb', line 43

def create_mapping_view(old_name, new_name, options = {})
  return unless supports_views?
  
  col_names = columns(old_name).collect { |col| col.name.to_sym }
  mapper = MappingDefinition.new(col_names)
  
  yield mapper
  
  if options[:force]
    drop_view(new_name) rescue nil
  end

  view_sql = "CREATE VIEW #{new_name} "
  if supports_view_columns_definition?
    view_sql << "(#{mapper.view_cols.collect { |c| quote_column_name(c) }.join(', ')}) "
  end
  view_sql << "AS SELECT #{mapper.select_cols.collect { |c| quote_column_name(c) }.join(', ')} FROM #{old_name}"
  execute view_sql
end

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

:replace

Specify we should use CREATE OR REPLACE VIEW not just CREATE VIEW



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb', line 15

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

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

    create_sql = options[:replace] ? "CREATE OR REPLACE VIEW " : "CREATE VIEW "
    create_sql << "#{quote_table_name(name)} "
    if supports_view_columns_definition? && !view_definition.to_sql.blank?
      create_sql << "("
      create_sql << view_definition.to_sql
      create_sql << ") " 
    end
    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_table_with_cascade(table_name, options = {}) ⇒ Object



63
64
65
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb', line 63

def drop_table_with_cascade(table_name, options = {})
  execute "DROP TABLE #{quote_table_name(table_name)} CASCADE"
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.



72
73
74
75
76
77
78
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb', line 72

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