module RailsSqlViews4::ConnectionAdapters::SchemaStatements

Public Class Methods

included(base) click to toggle source
# File lib/rails_sql_views4/connection_adapters/abstract/schema_statements.rb, line 4
def self.included(base)
  base.alias_method_chain :drop_table, :cascade
end

Public Instance Methods

create_mapping_view(old_name, new_name, options = {}) { |mapper| ... } click to toggle source

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.

# File lib/rails_sql_views4/connection_adapters/abstract/schema_statements.rb, line 62
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_or_create_view_base(name, select_query, options, init_create_sql) { |view_definition| ... } click to toggle source
# File lib/rails_sql_views4/connection_adapters/abstract/schema_statements.rb, line 29
def create_or_create_view_base(name, select_query, options, init_create_sql)
  if supports_views?
    view_definition = ViewDefinition.new(self, select_query)
    
    if block_given?
      yield view_definition
    end

    if options[:force]
      puts" TOTO : Please, force option is not correctly implemented in postgres"
      drop_view(name) rescue nil
    end

    # create_sql = "CREATE VIEW "
    create_sql =  init_create_sql
    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
create_or_replace_view(name, select_query, options={}) click to toggle source
# File lib/rails_sql_views4/connection_adapters/abstract/schema_statements.rb, line 25
def create_or_replace_view(name, select_query, options={})
  create_or_create_view_base(name, select_query, options, "CREATE OR REPLACE VIEW ")
end
create_view(name, select_query, options={}) click to toggle source

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.

we have a problem with the force option in postgres rescue nil will rescue the ruby program, but we still have an error for postgres and unfortunatly for postgress, the first error will stop the interpretor : ActiveRecord::StatementInvalid Exception: PG::InFailedSqlTransaction: ERROR:

current transaction is aborted, commands ignored until end of transaction block

# File lib/rails_sql_views4/connection_adapters/abstract/schema_statements.rb, line 21
def create_view(name, select_query, options={})
  create_or_create_view_base(name, select_query, options, "CREATE VIEW ")
end
drop_table_with_cascade(table_name, options = {}) click to toggle source

TODO : est appelé avec SQLITE, alors que c’est pas ok

# File lib/rails_sql_views4/connection_adapters/abstract/schema_statements.rb, line 83
def drop_table_with_cascade(table_name, options = {})
  # execute "DROP TABLE #{quote_table_name(table_name)} CASCADE"
  if supports_drop_table_cascade? 
    execute "DROP TABLE #{quote_table_name(table_name)} CASCADE"
  else
    execute "DROP TABLE #{quote_table_name(table_name)}"
  end
end
drop_view(name, options={}) click to toggle source

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.

# File lib/rails_sql_views4/connection_adapters/abstract/schema_statements.rb, line 97
def drop_view(name, options={})
  # if supports_views?  # because of postgres (see create_view with force option)
  if supports_views? & table_exists?(name)
    drop_sql = "DROP VIEW #{quote_table_name(name)}"
    drop_sql << " #{options[:drop_behavior]}" if options[:drop_behavior]
    execute drop_sql
  end
end